java优化上传速度慢怎么办_为什么在java 7中ftp上传缓慢?

I wanted to ask if anyone knows about any Java 7 issues with FTP? I've used both the Sun Net and Apache Commons Net libraries and both perform as expected on Java 6. But when I switch my dev environment (Eclipse) to 1.7, the same operations perform really slow (about 4.5 to 8KB/s), and these are to localhost servers and another server within the LAN.

I've tried buffered streams, byte-to-byte transfer, turning the Nagle Algorithm off, and using the Apache convenience method storeFile(), with the latter finally performing to speed on localhost but slowing down again to a crawl on a remote server. I also set all machines to turn off stateful FTP filtering.

InputStream is = null;

OutputStream os = null;

try {

is = new BufferedInputStream(prepareInputStream(data));

os = new BufferedOutputStream(prepareOutputStream(data));

if (is == null || os == null) {

log.error("Can't build connection");

return;

}

byte[] buf = new byte[4096];

int c = 1;

while (c > 0) {

c = is.read(buf);

if (c > 0)

os.write(buf, 0, c);

data.incrCurrentPosition();

fireStateChanged(data);

}

data.incrCurrentPosition();

} catch (IOException e) {

log.error(e.getMessage(), e);

setEnabled(false);

} catch (Exception e) {

log.error(e.getMessage(), e);

} finally {

if (is != null) {

try {

is.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if (os != null) {

try {

os.close();

} catch (IOException e) {

e.printStackTrace();

}

}

As can be seen, this is pretty standard implementation code. Again, in Java 6, things zip by really quick. In Java 7, it slows down by a factor of 10 to 20 for both the Sun and Apache Commons libraries. Using an FTP client like FileZilla confirms that FTP is functioning normally, so I think it really has something to do with Java 7. I dug as far as I could online for any mention of a problem but, mostly, the things I saw were about the Java 7 and Windows 7 firewall conflict.

Thanks in advance for any insight given.

解决方案

Please check what your current buffer size is with :

ftpClient.getBufferSize();

If you haven't already set it to something else, that will be zero (0).

So, set it to a higher value :

ftpClient.setBufferSize(1048576);//1024*1024

You can check its current value as before :

ftpClient.getBufferSize();

By the way, the accepted answer, setBufferSize(0), did not work for me. I use the latest version of Apache commons, so probably that solution worked with earlier versions. If you set buffer size to zero, there will be no change with the current version.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值