zoukankan      html  css  js  c++  java
  • 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; }
  • 相关阅读:
    20080619 SQL SERVER 输入 NULL 的快捷键
    20090406 Adobe的“此产品的许可已停止工作”错误的解决办法
    20080908 Office Powerpoint 2007 不能输入中文的解决办法
    20080831 ClearGertrude Blog Skin 's cnblogs_code class
    20080603 Facebook 平台正式开放
    20080519 安装 Microsoft SQL Server 2000 时提示 创建挂起的文件操作
    test
    Linux—fork函数学习笔记
    SOA的设计理念
    Why BCP connects to SQL Server instance which start with account of Network Service fail?
  • 原文地址:https://www.cnblogs.com/lizm166/p/9953032.html
Copyright © 2011-2022 走看看