如何提高多个文件的ftp上传速度[Java]
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何提高多个文件的ftp上传速度[Java],小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5612字,纯文字阅读大概需要9分钟。
内容图文
![如何提高多个文件的ftp上传速度[Java]](/upload/InfoBanner/zyjiaocheng/771/563f1b93c15e48c8b8b0ce98a7a5475e.jpg)
我使用org.apache.commons.net.ftp.FTPClient实现了java代码将文件上传到服务器
对于多个文件,ftp上传速度非常慢.
我怎样才能提高速度.
– 改变图书馆?
用于上传多个文件的强大FTP客户端类库是什么?
– 使用多个线程?
如何用多线程实现ftp上传功能?
有人能告诉我一个例子吗?
我是多线程编程的新手.
在我阅读完所有答案后,我尝试更改我的代码并进行测试.
以下是一个示例FTPClient代码:
// create instance of FTPClient
FTPClient ftp = new FTPClient();
ftp.setControlEncoding("UTF-8");
ftp.setDefaultTimeout(30000);
// connect to server
try
{
ftp.connect("10.1.1.1", 990);
}
catch(Exception e)
{
System.out.println("Cannot connect to server");
return;
}
// login to server
if (!ftp.login("username", "password"))
{
ftp.logout();
System.out.println("Cannot login to server");
return;
}
try
{
ftp.setFileTransferMode(FTP.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
// ftp.setBufferSize(0); <-- someone suggest me to set buffer size to 0, but it throw error sometime.
}
catch(Exception e)
{
}
// create directory on server
// dirs is list of required directories on server
for (String dir : dirs)
{
try
{
ftp.makeDirectory(dir);
}
catch(IOException e)
{
}
}
// files is a map of local file and string of remote file
// such as
// file on client is "C://test/a.txt"
// location on server is "/test/a.txt"
for (Map.Entry<File, String> entry : files.entrySet())
{
File localFile = entry.getKey();
String remoteFile = entry.getValue();
FileInputStream input = null;
try
{
input= new FileInputStream(localFile);
ftp.storeFile(remoteFile, input);
}
catch (Exception e)
{
try
{
ftp.deleteFile(remoteFile);
}
catch (IOException e1)
{
}
}
finally
{
if (input != null)
{
try
{
input.close();
}
catch (IOException e)
{
}
}
}
}
// disconnect
if (ftp != null && ftp.isConnected())
{
try
{
ftp.disconnect();
}
catch (IOException f)
{
// do nothing
}
}
当我上传1050个文件(每个文件大约1-20 KB)时,它花了大约49406 – 51000毫秒(这只是上传时间).
我想提高速度.
有人建议我使用ftp4j,但是当我用1050个文件测试库时,ftp4j的上传速度比FTPClient慢大约10000毫秒.
花了大约60000毫秒.
以下是示例ftp4j代码:
// create instance of FTPClient
FTPClient ftp = new FTPClient();
ftp.setCharset("UTF-8");
// connect to server
try
{
ftp.connect("10.1.1.1", 990);
}
catch(Exception e)
{
System.out.println("Cannot connect to server")
return;
}
// login to server
try
{
ftp.login("username", "password");
}
catch (Exception e)
{
try
{
ftp.logout();
}
catch (Exception e1)
{
}
System.out.println("Cannot login to server")
return;
}
try
{
ftp.setType(FTPClient.TYPE_BINARY);
ftp.setPassive(true);
}
catch(Exception e)
{
}
// create directory on server
// dirs is list of required directories on server
for (String dir : dirs)
{
try
{
ftp.createDirectory(dir);
}
catch (Exception e)
{
}
}
// files is a map of local file and string of remote file
// such as
// file on client is "C://test/a.txt"
// location on server is "/test/a.txt"
for (Map.Entry<File, String> entry : files.entrySet())
{
final File localFile = entry.getKey();
final String remoteFile = entry.getValue();
BufferedInputStream input = null;
boolean success = false;
try
{
input = new BufferedInputStream(new FileInputStream(localFile));
// ftp.upload(localFile); <-- if I use ftp.upload(File), it will took more time.
ftp.upload(remoteFile, input, 0, 2048, new MyTransferListener());
success = true;
}
catch (Exception e)
{
}
finally
{
if (input != null)
{
try
{
input.close();
}
catch (IOException e)
{
}
}
if (!success)
{
try
{
ftp.deleteFile(remoteFile);
}
catch (Exception e)
{
}
}
}
}
// disconnect
if (ftp != null && ftp.isConnected())
{
try
{
ftp.disconnect();
}
catch (IOException f)
{
// do nothing
}
}
我尝试使用多个线程.
以下是多线程代码:
final CountDownLatch latch = new CountDownLatch(files.size());
ExecutorService pool = Executors.newFixedThreadPool(10);
for (Map.Entry<File, String> entry : files.entrySet())
{
final File localFile = entry.getKey();
final String remoteFile = entry.getValue();
pool.execute(new Runnable() {
public void run()
{
FileInputStream input = null;
try
{
input= new FileInputStream(localFile);
ftp.storeFile(remoteFile, input);
}
catch (Exception e)
{
try
{
ftp.deleteFile(remoteFile);
}
catch (IOException e1)
{
}
}
finally
{
if (input != null)
{
try
{
input.close();
}
catch (IOException e)
{
}
}
latch.countDown();
}
}
});
}
try
{
// waiting for all threads finish
// see: https://stackoverflow.com/questions/1250643/how-to-wait-for-all-threads-to-finish-using-executorservice
latch.await();
}
catch(Exception e)
{
}
这是对的吗?它工作正常,但无法提高速度.
它花了大约49000 – 51000毫秒与没有线程的代码相同.
我用内联网测试速度.互联网需要更多时间.
我该怎么做才能提高上传速度?
解决方法:
我不知道为什么,但Apache Commons FTP上传速度相当慢,我遇到了同样的问题,我无法解决它.
现在我使用FTP4j,它非常类似于apache commons ftp,但上传速度非常快.
这是一个例子:
FTPClient client = new FTPClient();
client.connect("www.yoursite.com");
client.login("login", "password");
client.setPassive(true);
client.setType(FTPClient.TYPE_BINARY);
client.changeDirectory("a");
File f = new File("path/to/your/file");
client.upload(f);
client.disconnect(true);
有了这个库,我在不到一秒的时间内就获得了一个340KB的文件,而使用Apache Commons FTP需要大约1分钟.
如果要使用线程传输不同的文件,请尝试将每个client.upload(f)放入不同的线程,但我不确定它是否会增加传输速度.
引用@fge上一个答案:
基本上,机会是,你不能.
不要忘记FTP有两种类型的通道:命令通道和数据通道.通过在命令通道上发送指令来启动一次上载,以打开正确上载的数据通道.
现在:
>大多数FTP服务器都配置为一个命令通道只能在任何时间打开一个数据通道;
>有带宽限制:您的上游带宽和服务器的下游带宽.
如果可以并行上传多个文件,即打开多个数据通道,您就会遇到TCP本身的开销实际上会降低上传过程的问题.
基本上:随时保持一个数据通道打开.尝试和打开不止一个是不值得的.一般来说,它可能在约1%的情况下起作用.这不值得麻烦.
内容总结
以上是互联网集市为您收集整理的如何提高多个文件的ftp上传速度[Java]全部内容,希望文章能够帮你解决如何提高多个文件的ftp上传速度[Java]所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。