gpt4 book ai didi

java - Apache Commons Net FTP 正在上传损坏的文件

转载 作者:行者123 更新时间:2023-12-01 17:20:06 25 4
gpt4 key购买 nike

我正在尝试使用 Apache Commons Net 进行 FTP 文件传输。

问题是文件间歇性到达服务器时已损坏。我所说的“损坏”是指 WinRAR 告诉我 ZIP 文件有一个“存档意外结束”。有时文件完全是空的。我注意到对于较大的文件(100kb+)来说这种情况更常见,但是对于小文件(20kb)也会发生这种情况。

我知道上传的源 zip 文件是有效的,并且只有 243kb。

我没有从代码中得到任何错误/异常。

这是正在执行的代码:

int CON_TIMEOUT = (int) TimeUnit.SECONDS.toMillis(20); // fail if can't connect within 20 seconds
int LIVE_TIMEOUT = (int) TimeUnit.MINUTES.toMillis(5); // allow up to 5 minutes for data transfers

FTPClient client = new FTPClient();
client.setConnectTimeout(CON_TIMEOUT);
client.setDataTimeout(LIVE_TIMEOUT);
client.connect(host);
client.setSoTimeout(LIVE_TIMEOUT);
client.login(user, pass);
client.changeWorkingDirectory(dir);
log("client ready");

File file = new File(filePath);
String name = new Date().getTime() + "-" + file.getName();

InputStream fis = null;
try
{
fis = new FileInputStream(file);
if (!client.storeFile(name, fis))
throw new RuntimeException("store failed");
log("store " + name + " complete");
}
finally
{
IOUtils.closeQuietly(fis);
try
{
client.logout();
log("logout");
}
catch (Throwable e)
{
log("logout failed", e);
}
try
{
client.disconnect();
log("disconnect");
}
catch (Throwable e)
{
log("disconnect failed", e);
}
}

和一些日志:

2010-08-10 21:32:38 client ready
2010-08-10 21:32:49 store 1281439958234-file.zip complete
2010-08-10 21:32:49 logout
2010-08-10 21:32:49 disconnect
2010-08-10 21:32:50 client ready
2010-08-10 21:33:00 store 1281439970968-file.zip complete
2010-08-10 21:33:00 logout
2010-08-10 21:33:00 disconnect
2010-08-10 21:33:02 client ready
2010-08-10 21:33:11 store 1281439982234-file.zip complete
2010-08-10 21:33:11 logout
2010-08-10 21:33:11 disconnect
2010-08-10 21:33:15 client ready
2010-08-10 21:33:25 store 1281439995890-file.zip complete
2010-08-10 21:33:26 logout
2010-08-10 21:33:26 disconnect
2010-08-10 21:33:27 client ready
2010-08-10 21:33:36 store 1281440007531-file.zip complete
2010-08-10 21:33:36 logout
2010-08-10 21:33:36 disconnect
2010-08-10 21:33:37 client ready
2010-08-10 21:33:48 store 1281440017843-file.zip complete
2010-08-10 21:33:48 logout
2010-08-10 21:33:48 disconnect
2010-08-10 21:33:49 client ready
2010-08-10 21:33:59 store 1281440029781-file.zip complete
2010-08-10 21:33:59 logout
2010-08-10 21:33:59 disconnect
2010-08-10 21:34:00 client ready
2010-08-10 21:34:09 store 1281440040812-file.zip complete
2010-08-10 21:34:09 logout
2010-08-10 21:34:09 disconnect
2010-08-10 21:34:10 client ready
2010-08-10 21:34:23 store 1281440050859-file.zip complete
2010-08-10 21:34:24 logout
2010-08-10 21:34:24 disconnect
2010-08-10 21:34:25 client ready
2010-08-10 21:34:35 store 1281440065421-file.zip complete
2010-08-10 21:34:35 logout
2010-08-10 21:34:35 disconnect

请注意,所有这些都在 15 秒内完成,并且服务器上的所有生成文件都已损坏。

我也测试过,没有设置任何超时,问题仍然出现。

最佳答案

Commons FTP 默认为 Ascii 文件类型。当处理 ZIP 文件等二进制数据时,您希望将其设置为 Binary。

来自http://commons.apache.org/net/api/org/apache/commons/net/ftp/FTPClient.html

The default settings for FTPClient are for it to use FTP.ASCII_FILE_TYPE , FTP.NON_PRINT_TEXT_FORMAT , FTP.STREAM_TRANSFER_MODE , and FTP.FILE_STRUCTURE . The only file types directly supported are FTP.ASCII_FILE_TYPE and FTP.BINARY_FILE_TYPE .

您需要在发送文件之前执行setFileType(FTP.BINARY_FILE_TYPE)

关于java - Apache Commons Net FTP 正在上传损坏的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61310674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com