Java Ftp 操做上傳與下載

  1. import java.io.File;  
  2. import java.io.FileInputStream;  
  3. import java.io.FileOutputStream;  
  4. import java.io.IOException;  
  5.   
  6. import org.apache.commons.io.IOUtils;  
  7. import org.apache.commons.net.ftp.FTPClient;  
  8.   
  9. import sun.net.TelnetInputStream;  
  10. import sun.net.TelnetOutputStream;  
  11. import sun.net.ftp.FtpClient;  
  12.   
  13.   
  14. public class Test {  
  15.     private String ip = "192.168.5.200";  
  16.     private int poot = 21;  
  17.     private String userName = "weizheng";  
  18.     private String password = "123456";  
  19.     private String oldFilePath = "C://Users//Administrator//Desktop//aaa.doc";  
  20.     private String newFileName = "123456.doc";  
  21.     private String dowloadPath = "F:/"+newFileName;  
  22.       
  23.     private void apacheUpload() throws Exception{  
  24.         FTPClient client = new FTPClient();    
  25.         FileInputStream fis = null;   
  26.           
  27.         client.connect(ip);  
  28.         client.login(userName, password);  
  29.         File srcFile = new File(oldFilePath);   
  30.         fis = new FileInputStream(srcFile);   
  31.         //設置上傳目錄   
  32.         client.changeWorkingDirectory("/");   
  33.         client.setBufferSize(1024);   
  34.         client.setControlEncoding("GBK");   
  35.         //設置文件類型(二進制)   
  36.         client.setFileType(FTPClient.BINARY_FILE_TYPE);   
  37.         client.storeFile("abc.doc", fis);   
  38.           
  39.         IOUtils.closeQuietly(fis);   
  40.         try {   
  41.             client.disconnect();   
  42.         } catch (IOException e) {   
  43.             e.printStackTrace();   
  44.             throw new RuntimeException("關閉FTP鏈接發生異常!", e);   
  45.         }   
  46.           
  47.     }  
  48.     private void apacheDowload() throws Exception{  
  49.         FTPClient ftpClient = new FTPClient();   
  50.         FileOutputStream fos = null;   
  51.           
  52.         try {   
  53.             ftpClient.connect(ip);   
  54.             ftpClient.login(userName, password);   
  55.             fos = new FileOutputStream(dowloadPath);   
  56.           
  57.             ftpClient.setBufferSize(1024);   
  58.             //設置文件類型(二進制)   
  59.             ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);   
  60.             ftpClient.retrieveFile(oldFilePath, fos);   
  61.         } catch (IOException e) {   
  62.             e.printStackTrace();   
  63.             throw new RuntimeException("FTP客戶端出錯!", e);   
  64.         } finally {   
  65.             IOUtils.closeQuietly(fos);   
  66.             try {   
  67.                 ftpClient.disconnect();   
  68.             } catch (IOException e) {   
  69.                 e.printStackTrace();   
  70.                 throw new RuntimeException("關閉FTP鏈接發生異常!", e);   
  71.             }   
  72.         }   
  73.           
  74.           
  75.     }  
  76.       
  77.       
  78.     private void sunUpload() throws Exception{  
  79.         FtpClient sunClient = new FtpClient();   
  80.         TelnetOutputStream os = null;    
  81.         FileInputStream is = null;    
  82.           
  83.         sunClient.openServer(ip);  
  84.         sunClient.login(userName, password);  
  85.         os = sunClient.put(newFileName);  
  86.           
  87.         File file = new File(oldFilePath);  
  88.         is = new FileInputStream(file);  
  89.         byte[] b = new byte[1024];  
  90.         int len = ;  
  91.         while((len=is.read(b))!=-1){  
  92.             os.write(b,,len);  
  93.         }  
  94.         is.close();  
  95.         os.close();  
  96.     }  
  97.     private void sunDownload() throws Exception{  
  98.         FtpClient sunClient = new FtpClient();   
  99.         TelnetInputStream is = null;    
  100.         FileOutputStream os = null;    
  101.         sunClient.openServer(ip);  
  102.         sunClient.login(userName, password);  
  103.         is = sunClient.get(newFileName);  
  104.           
  105.         File outputfile = new File(dowloadPath);  
  106.         os = new FileOutputStream(outputfile);  
  107.         byte[] b = new byte[1024];  
  108.         int len = ;  
  109.         while((len=is.read(b))!=-1){  
  110.             os.write(b, , len);  
  111.         }  
  112.         is.close();  
  113.         os.close();  
  114.     }  
  115.       
  116.     public static void main(String[] args) throws Exception {  
  117.         Test t = new Test();  
  118.         t.sunUpload();  
  119.         t.sunDownload();  
  120.           
  121. //      t.apacheUpload();  
  122. //      t.apacheDowload();  
  123.     }  
  124. }  
相關文章
相關標籤/搜索