Java代码实现ftp上传文件的两种方法对比

乔欣 Java经验 发布时间:2023-02-12 19:56:24 阅读数:10853 1
下文笔者讲述使用java代码进行ftp上传文件的方法及效率对比说明,如下所示

ftp上传文件的实现思路

方式1:
   使用二进制流进行文件传输
      此种方法拥有缓冲区,速度较快
方式2:
   使用storeFile()方法,进行文件传输,速度较慢
例:
ftp上传文件的示例
 
//使用二进制流传输
//设置缓冲区
//速度快
//100M的txt文件需25秒
 
//FTP传输到数据库服务器
public boolean uploadServerByFtp(String fileNmae){
  boolean flag = true;
   //客户端数据文件路径
   String client_path = "D://test/data/";
   //服务器上的存放数据文件路径
   String server_path = "/home/download/file_tmp/";
   String hostname = "8.8.8.8";
   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()方法
//未设置缓冲区,速度慢,100M的txt文件需要120秒
 
//FTP传输到数据库服务器
public boolean uploadServerByFtp(String fileNmae){
  boolean flag = true;
   //客户端数据文件路径
   String client_path = "D://test/data/";
   //服务器上的存放数据文件路径
   String server_path = "/home/download/file_tmp/";
   String hostname = "8.8.8.8";
   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;
}
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202302/16762033885756.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者