commons-net ftp正在上传损坏的文件 [英] commons-net ftp is uploading corrupted files

查看:159
本文介绍了commons-net ftp正在上传损坏的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用apache commons-net进行ftp文件传输。

问题是文件间歇性地到达服务器损坏处。通过'腐败'我的意思是,WinRAR告诉我一个zip文件有'意外的存档结束'。有时文件是完全空的。我注意到这种情况发生在更大的文件(100kb +)上,但是对于小文件也是如此(20kb)。我知道一个事实,即源zip文件上传有效,只有243kb。

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



这是执行的代码:

  try 
{
int CON_TIMEOUT =(int)TimeUnit。 SECONDS.toMillis(20); //失败,如果在20秒内无法连接
int LIVE_TIMEOUT =(int)TimeUnit.MINUTES.toMillis(5); //允许长达5分钟的数据传输

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(客户端就绪);

档案档案=新档案(filePath);
String name = new Date()。getTime()+ - + file.getName();

InputStream fis = null;
尝试
{
fis = new FileInputStream(file);
if(!client.storeFile(name,fis))
throw new RuntimeException(store failed);
log(store+ name +complete);
}
finally
{
IOUtils.closeQuietly(fis);
尝试
{
client.logout();
log(logout);

catch(Throwable e)
{
log(注销失败,e);
}
尝试
{
client.disconnect();
log(disconnect);

catch(Throwable e)
{
log(disconnect failed,e);



catch(Throwable e)
{
log(transfer 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注销
2010-08-10 21:32:49断开
2010-08-10 21:32:50客户端准备好
2010-08-10 21:33:00商店1281439970968-file.zip完成
2010-08-10 21:33:00注销
2010-08-10 21:33:00断开
2010-08- 10 21:33:02客户端准备好
2010-08-10 21:33:11商店1281439982234-file.zip完成
2010-08-10 21:33:11注销
2010- 08-10 21:33:11断开
2010-08-10 21:33:15客户端准备好
2010-08-10 21:33:25存储1281439995890-file.zip完成
2010-08-10 21:33:26注销
2010-08-10 21:33:26 disconnect
2010-08-10 21:33:27客户端准备好
2010-08- 10 21:33:36商店1281440007531-file.zip完成
2010-08-10 21:33:36注销
2010-08-10 21:33:36断开
2010-08 -10 21:33:37客户端准备好
2010-08-10 21:33:48商店1281 440017843-file.zip完成
2010-08-10 21:33:48注销
2010-08-10 21:33:48断开
2010-08-10 21:33:49客户端准备好
2010-08-10 21:33:59商店1281440029781-file.zip完成
2010-08-10 21:33:59注销
2010-08-10 21:33 :59 disconnect
2010-08-10 21:34:00客户端准备好
2010-08-10 21:34:09商店1281440040812-file.zip完成
2010-08-10 21 :34:09注销
2010-08-10 21:34:09断开
2010-08-10 21:34:10客户端准备好
2010-08-10 21:34:23商店1281440050859-file.zip完成
2010-08-10 21:34:24注销
2010-08-10 21:34:24断开
2010-08-10 21:34: 25客户端准备好
2010-08-10 21:34:35商店1281440065421-file.zip完成
2010-08-10 21:34:35注销
2010-08-10 21: 34:35断开连接

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



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

解决方案

Commons FTP默认为ascii文件类型。你想在处理二进制数据时将它设置为Binary。



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


FTPClient的默认设置是为了使用FTP.ASCII_FILE_TYPE,FTP.NON_PRINT_TEXT_FORMAT,FTP.STREAM_TRANSFER_MODE和FTP。 FILE_STRUCTURE。唯一直接支持的文件类型是FTP.ASCII_FILE_TYPE和FTP.BINARY_FILE_TYPE。


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


I'm trying to use apache commons-net for ftp file transfers.

Problem is files are intermittently arriving at the server corrupt. by 'corrupt' i mean that winrar tells me a zip file has an 'Unexpected end of archive'. sometimes the files are completely empty. I have noticed that this happens more for larger files (100kb+), however does happen for small files too (20kb).

I know for a fact that the source zip file being uploaded is valid, and is only 243kb.

I do not get any errors/exceptions from the code.

Here's the code being executed:

    try
    {
        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);
            }
        }
    }
    catch (Throwable e)
    {
        log("transfer failed", e);
    }

and some logs:

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

note that all of these were complete within 15 seconds, and all of the resulting files on the server are corrupt.

i have also tested without setting any timeouts and the problem still occurs.

解决方案

Commons FTP defaults to ascii file types. You want to set it to Binary when dealing with binary data like a zip file.

From 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 .

you want to do setFileType(FTP.BINARY_FILE_TYPE) before you send the file.

这篇关于commons-net ftp正在上传损坏的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆