加速Apache Commons FTPClient传输
我使用Apache Commons FTPClient上传大文件,但传输速度只是使用WinSCP通过FTP传输速度的一小部分。 我怎样才能加快转移?
public boolean upload(String host, String user, String password, String directory, String sourcePath, String filename) throws IOException{ FTPClient client = new FTPClient(); FileInputStream fis = null; try { client.connect(host); client.login(user, password); client.setControlKeepAliveTimeout(500); logger.info("Uploading " + sourcePath); fis = new FileInputStream(sourcePath); // // Store file to server // client.changeWorkingDirectory(directory); client.setFileType(FTP.BINARY_FILE_TYPE); client.storeFile(filename, fis); client.logout(); return true; } catch (IOException e) { logger.error( "Error uploading " + filename, e ); throw e; } finally { try { if (fis != null) { fis.close(); } client.disconnect(); } catch (IOException e) { logger.error("Error!", e); } } }
增加缓冲区大小:
client.setBufferSize(1024000);
使用outputStream方法,并使用缓冲区传输。
InputStream inputStream = new FileInputStream(myFile); OutputStream outputStream = ftpclient.storeFileStream(remoteFile); byte[] bytesIn = new byte[4096]; int read = 0; while((read = inputStream.read(bytesIn)) != -1) { outputStream.write(bytesIn, 0, read); } inputStream.close(); outputStream.close();
有一个已知的Java 1.7和Commons Net 3.2,该bug是https://issues.apache.org/jira/browse/NET-493
如果运行这些版本,我建议升级到Commons Net 3.3作为第一步。 显然3.4修复了更多的性能问题。
如果你使用ftp.setbuffersize(0)会更好; 如果你使用0作为缓冲区大小,它将采用无限缓冲区大小。 显然你的交易会加速……我个人经历过……一切都很好…… 🙂
- 如何使用commons dbutils生成密钥?
- org.apache.commons.net.ftp.FTPClient listFiles()的问题
- Apache Commons CLI:获取选项的值列表
- apache commons net ssl握手错误与android
- 是否有StringUtils.isNumeric的替代方法,我的意思是什么?
- Apache commons fileupload“Streaming API”
- Spring Framework是否支持多部分请求中的流模式
- 在GenericObjectPool中创建对象
- Apache Commons DbUtils的替代轻量级JDBC帮助库