問題描述
我想問是否有人知道有關 FTP 的任何 Java 7 問題?我使用了 Sun Net 和 Apache Commons Net 庫,并且在 Java 6 上都按預期執行.但是當我將開發環境 (Eclipse) 切換到 1.7 時,相同的操作執行速度非常慢(大約 4.5 到 8KB/s),這些是本地主機服務器和局域網內的另一臺服務器.
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.
我嘗試過緩沖流、字節到字節傳輸、關閉 Nagle 算法,并使用 Apache 便捷方法 storeFile(),后者最終在 localhost 上執行加速,但再次減速到爬行遠程服務器.我還將所有機器都設置為關閉有狀態的 FTP 過濾.
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();
}
}
可以看出,這是非常標準的實現代碼.同樣,在 Java 6 中,事情進展得非常快.在 Java 7 中,Sun 和 Apache Commons 庫的速度都降低了 10 到 20 倍.使用像 FileZilla 這樣的 FTP 客戶端可以確認 FTP 運行正常,所以我認為它確實與 Java 7 有關.我盡我所能在網上搜索任何提到的問題,但大多數情況下,我看到的都是關于Java 7 和 Windows 7 防火墻沖突.
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();
如果您尚未將其設置為其他值,則該值將為零 (0).因此,將其設置為更高的值:
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();
順便說一句,接受的答案 setBufferSize(0) 對我不起作用.我使用最新版本的 Apache commons,因此該解決方案可能適用于早期版本.如果將緩沖區大小設置為零,則當前版本不會發生任何變化.
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.
這篇關于為什么在 java 7 中 ftp 上傳速度很慢的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!