请问java有办法实现通过互联网向另外一台机器传送文件的办法么

解决方案 »

  1.   


    public   void   acceptFile   (BufferedOutputStream   bos,Socket   sk)   
        {   
      //线程运行实体     
          BufferedReader   in   =   null;   
          DataInputStream   ins   =   null;   
          try{   
              InputStreamReader   isr;   
              isr   =   new   InputStreamReader   (sk.getInputStream   ());   
              in   =   new   BufferedReader   (isr);   
              ins   =   new   DataInputStream(new   BufferedInputStream(sk.getInputStream()));   
                              byte[]   buf=   new   byte[65536];   
                              indata=0;     
                                
                              inti=System.currentTimeMillis();   
                              indata=ins.read(buf);   
              while(indata!=-1){   
      bos.write(buf,0,indata);   
                  bos.flush();   
                  indata   =   ins.read(buf);   
              }   
              setEnableAll(true);   
              ifcom=false;   
              jd1.dispose();   
              bos.close();     
              ins.close();   
                
              }catch   (IOException   e)   
              {   
                    System.out.println   (e.toString   ()+"传输出现异常");   
              }   
              finally   
              {   
                  try{   
                    if   (in   !=   null)   
                        in.close   ();   
                      if   (sk   !=   null)   
                            sk.close   ();     
            }catch   (IOException   e)   
              {   
                  System.out.println("流设备关闭错误!");   
              }   
                
              }   
        }   
      }   
        
        
        
      public   void   fileSend   ()   
        {   
          try{   
          //使用port端口   
            sk   =   new   Socket   ();   
            InetSocketAddress   ia   =   new   InetSocketAddress(addr,port);   
            sk.connect(ia,2000);   
              
                              InputStreamReader   isr;   
            isr   =   new   InputStreamReader   (sk.getInputStream   ());   
            in   =   new   BufferedReader   (isr);   
            //建立输出   
            out   =   new   PrintWriter   (   
                          new   BufferedWriter(   
                            new   OutputStreamWriter(   
                                sk.getOutputStream   ())),   true);   
                              outs   =   new   DataOutputStream(new   BufferedOutputStream(sk.getOutputStream()));   
                              File   file   =   new   File(filename);   
                              long   flength   =   file.length();   
                              FileInputStream   fis   =   new   FileInputStream(file);       
                              BufferedInputStream   bis =   new   BufferedInputStream(fis);   
                              byte[]   buf   =   new   byte[65536];   
                              outdata=0;   
                              n   =   fis.available();   
            inti=System.currentTimeMillis();   
            outdata=bis.read(buf);   
        
      while(outdata!=-1){   
      outs.write(buf,0,outdata);             //发送数据   
      outs.flush();   
      outdata=bis.read(buf);   
      }   
            jb1.setEnabled(true);   
            if(bis!=null)bis.close();   
          }   
          catch   (IOException   e)   
          {   
      JOptionPane.showMessageDialog(null,"连接出错!");   
      jl3.setText("连接服务器出错!网络错误!请重试!");   
      jb1.setEnabled(true);   
          }   
          finally   
          {   
            //释放资源   
            try   
            {   
              if   (in   !=   null)   in.close   ();   
              if   (out   !=   null)   out.close   ();   
              if   (sk   !=   null)   sk.close   ();   
            }   
            catch   (IOException   e)   
            {   
            }   
          }   
        }
    参考这个http://topic.csdn.net/t/20060314/07/4612346.html