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; }
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/16688.html