Java ftp上傳文件方法效率對比


Java ftp上傳文件方法效率對比

一、功能簡介:

txt文件采用ftp方式從windows傳輸到Linux系統;

二、ftp實現方法

(1)方法一:采用二進制流傳輸,設置緩沖區,速度快,50M的txt文件需要15秒;

//FTP傳輸到數據庫服務器
public boolean uploadServerByFtp(String fileNmae){
  boolean flag = true;
   //客戶端數據文件路徑
   String client_path = "D://answer/data/";
   //服務器上的存放數據文件路徑
   String server_path = "/home/download/file_tmp/";
   String hostname = "192.25.125.112";
   String ftpusername =  "root";
   String ftppwd = “123456”;
   int port = 21;//查找路徑下的指定txt文件,然后采用FTP上傳
   File file_name = new File(client_path+fileNmae);  
   if(!file_name.exists()){
     return false;
   }
   //創建ftp客戶端
   FTPClient ftpClient = new FTPClient();
   ftpClient.setControlEncoding("utf-8");
   //主動模式
   ftpClient.enterLocalActiveMode();
   String getfileName = file_name.getName();
   String getfileNamePath = file_name.getPath();
   if((getfileName.substring(getfileName.lastIndexOf(".")).trim().equals(".txt"))){
     try {
        //鏈接ftp服務器
         ftpClient.connect(hostname, port);
         //登錄ftp
         ftpClient.login(ftpusername, ftppwd);
         int reply = ftpClient.getReplyCode();
         if (!FTPReply.isPositiveCompletion(reply)) {
            ftpClient.disconnect();
             logger.info("Returns a 530 password username error or the current user does not have permission to close the FTP connection");
             return false;
         }
         ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
         String server_file_name = server_path+ getfileName;
InputStream input = new FileInputStream(getfileNamePath); OutputStream out = ftpClient.storeFileStream(server_file_name); byte[] byteArray = new byte[4096]; int read = 0; while ((read = input.read(byteArray)) != -1) {   out.write(byteArray, 0, read); } out.close(); ftpClient.logout(); } catch (SocketException e) { flag = false; e.printStackTrace(); } catch (IOException e) { flag = false; e.printStackTrace(); } catch (Exception e) { flag = false; e.printStackTrace(); }finally {   if (ftpClient.isConnected()) {   try {   ftpClient.disconnect(); } catch (IOException ioe) { ioe.printStackTrace(); } } } } return flag; }

(2)方法二:storeFile()方法,沒有設置緩沖區,速度慢,50M的txt文件需要100秒;

//FTP傳輸到數據庫服務器
public boolean uploadServerByFtp(String fileNmae){
  boolean flag = true;
   //客戶端數據文件路徑
   String client_path = "D://answer/data/";
   //服務器上的存放數據文件路徑
   String server_path = "/home/download/file_tmp/";
   String hostname = "192.25.125.112";
   String ftpusername =  "root";
   String ftppwd = “123456”;
   int port = 21;
   //查找路徑下的指定txt文件,然后采用FTP上傳
   File file_name = new File(client_path+fileNmae);  
   if(!file_name.exists()){
     return false;
   }
   //創建ftp客戶端
   FTPClient ftpClient = new FTPClient();
   ftpClient.setControlEncoding("utf-8");
   String getfileName = file_name.getName();
   String getfileNamePath = file_name.getPath();
   if((getfileName.substring(getfileName.lastIndexOf(".")).trim().equals(".txt"))){
     try {
        //鏈接ftp服務器
         ftpClient.connect(hostname, port);
         //登錄ftp
         ftpClient.login(ftpusername, ftppwd);
         int reply = ftpClient.getReplyCode();
         if (!FTPReply.isPositiveCompletion(reply)) {
            ftpClient.disconnect();
             logger.info("Returns a 530 password username error or the current user does not have permission to close the FTP connection");
             return false;
         }
         ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
         String server_file_name = server_path+ getfileName;
//讀取源文件流(客戶端文件) InputStream client_fileInput = new FileInputStream(getfileNamePath); //傳送到服務端 ftpClient.storeFile(server_file_name, client_fileInput); client_fileInput.close(); ftpClient.logout(); } catch (SocketException e) { flag = false; e.printStackTrace(); } catch (IOException e) { flag = false; e.printStackTrace(); } catch (Exception e) { flag = false; e.printStackTrace(); }finally {   if (ftpClient.isConnected()) {   try {   ftpClient.disconnect(); } catch (IOException ioe) { ioe.printStackTrace(); } } } } return flag; }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM