我想问问是否有人知道 FTP 的 Java 7 问题?我同时使用了 Sun Net 和 Apache Commons Net 库,并且都在 Java 6 上按预期执行。但是当我将我的开发环境 (Eclipse) 切换到 1.7 时,相同的操作执行得非常慢(大约 4.5 到 8KB/s),这些是本地主机服务器和 LAN 中的另一台服务器。
我已经尝试过缓冲流、字节到字节传输、关闭 Nagle 算法以及使用 Apache 便利方法 storeFile(),后者最终在本地主机上执行速度很快,但在远程服务器。我还设置了所有机器以关闭状态 FTP 过滤。
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();
}
}
可以看出,这是非常标准的实现代码。同样,在 Java 6 中,事情变得非常快。在 Java 7 中,Sun 和 Apache Commons 库的速度都降低了 10 到 20 倍。使用像 FileZilla 这样的 FTP 客户端可以确认 FTP 正常运行,所以我认为它确实与 Java 7 有关。我尽可能地在网上搜索任何提到的问题,但大多数情况下,我看到的是关于Java 7 和 Windows 7 防火墙冲突。
提前感谢您提供的任何见解。
最佳答案
我找到了某种修复方法,至少足以让事情在 Java 7 中正常运行。我是通过使用 FTPClient 的 setBufferSize(0); 实现的,不幸的是,我认为没有类似的方法Sun 的 Java 7 的 Sun Net 实现中的方法。这对我来说并不重要,因为我对 Apache Commons Net 非常满意。希望 Oracle 会在适当的时候查明此事。
关于java - 为什么在 java 7 中 ftp 上传速度慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14000341/