JAVA 跨机器文件复制的二种方法


1.直接用SOCKET实现

server端:

  1. package com.taobao.terminator.allen.shellTest;   
  2. import java.io.DataInputStream;   
  3. import java.io.DataOutputStream;   
  4. import java.io.File;   
  5. import java.io.FileInputStream;   
  6. import java.io.FileNotFoundException;   
  7. import java.io.FileOutputStream;   
  8. import java.io.IOException;   
  9. import java.io.PrintStream;   
  10. import java.net.ServerSocket;   
  11. import java.net.Socket;   
  12. public class EcrmFileServerAllen {     
  13.     private final static int port = 18110;   
  14.     private final static int SIZE = 8192;   
  15.     private String basePath = "/home/admin/allen/file";   
  16. //  private String basePath = "d:/test";   
  17.     private  void send(){   
  18.         /**  
  19.          * 1.发送FileList  
  20.          * 2.接收FileName  
  21.          * 3.传递stream  
  22.          * 4.接收end,结束传递  
  23.          */  
  24.         ServerSocket server = null;   
  25.         Socket socket = null;   
  26.         DataInputStream  fileIn = null;   
  27.         DataOutputStream out = null;   
  28.         DataInputStream  in =null;   
  29.            
  30.         try {   
  31.             server = new ServerSocket(port);   
  32.             File files = new File(basePath);   
  33.             socket = server.accept();   
  34.             out = new DataOutputStream(socket.getOutputStream());   
  35.             in  = new DataInputStream(socket.getInputStream());   
  36.                
  37.             StringBuilder builder = new StringBuilder();   
  38.             for(String s : files.list()){   
  39.                 builder.append(s);   
  40.                 builder.append(",");   
  41.             }   
  42.                
  43.             System.out.println("------------send file------------");   
  44.             out.writeUTF(builder.toString());   
  45.             out.flush();   
  46.                
  47.             while(true){   
  48.                     System.out.println("------------begin read file------------");   
  49.                     String fileName = in.readUTF();   
  50.                     System.out.println("read file : " + fileName);   
  51.                     if("EOF".equals(fileName)){   
  52.                         System.out.println(fileName + "file send over ,thanks");   
  53.                         break;   
  54.                     }   
  55.                     File file = constuctNewFile(files.getAbsolutePath(),fileName);   
  56.                     fileIn = new DataInputStream(new FileInputStream(file));   
  57.                     System.out.println("the file: " + fileName + "length is " + file.length());   
  58.                     out.writeLong(file.length());   
  59.                     out.flush();   
  60.                        
  61.                     byte[] buffer = new byte[SIZE];   
  62.                     while(true){   
  63.                         int read = 0;   
  64.                         if(fileIn != null){   
  65.                             read = fileIn.read(buffer);   
  66.                         }   
  67.                         if ( read != -1){   
  68.                             out.write(buffer, 0, read);   
  69.                         } else {   
  70.                             break;   
  71.                         }   
  72.                     }          
  73.                        
  74.                     System.out.println("Write file over: " + fileName);   
  75.                     out.flush();   
  76.                     fileIn.close();   
  77.                     fileIn = null;   
  78.             }   
  79.             System.out.println("All is over");   
  80.         } catch (IOException e) {   
  81.             throw new RuntimeException("IO传输异常",e);   
  82.         } finally{   
  83.             try {   
  84.                 out.close();   
  85.                 socket.close();   
  86.                 server.close();   
  87.             } catch (IOException e) {   
  88.                 throw new RuntimeException("IO关闭异常",e);   
  89.             }   
  90.         }   
  91.     }   
  92.        
  93.     private File constuctNewFile(String filePath , String fileName){   
  94.         fileName = filePath + File.separator + fileName;   
  95.         return new File(fileName);   
  96.     }   
  97.        
  98.        
  99.     public static void main(String arg[]) {   
  100.         PrintStream myout = null;   
  101.         try {   
  102.             myout = new PrintStream(new FileOutputStream(new File("/home/admin/main.log")));   
  103.         } catch (FileNotFoundException e) {   
  104.             e.printStackTrace();   
  105.         }           
  106.         System.setOut(myout);            
  107.         System.setErr(myout);    
  108.         new EcrmFileServerAllen().send();   
  109.     }   
  110. }  
  • 1
  • 2
  • 3
  • 4
  • 下一页

相关内容