从“某"下载的图像FTPClient的服务器已损坏 [英] Images downloaded from "some" servers with FTPClient are corrupted

查看:254
本文介绍了从“某"下载的图像FTPClient的服务器已损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从Java中的FTP服务器下载.png文件. 我有3个不同的服务器,每个服务器都包含一个文件夹,该文件夹具有完全相同的.png文件.

I need to download .png files from a FTP server in Java. I have 3 different servers, each one contain a folder with exactly the same .png files.

在服务器1上:
如果我使用FTPClient(apache.commons.net.ftp)下载存储在此服务器上的4686字节的.png文件,则会得到4706字节的.png文件,但无法打开. 如果使用Total Commander下载它,则会得到一个4686字节的.png文件,并且可以打开它.

On server 1 :
If I download my .png file of 4686 bytes stored on this server with FTPClient (apache.commons.net.ftp), I get a 4706 bytes .png file, and I can't open it. If I download it with Total Commander, I get a 4686 bytes .png file, and I can open it.

在服务器2和3上:
使用FTPClient和Total Commander,在这两种情况下,我都得到一个4686字节的文件,我可以毫无问题地打开它.

On server 2 and 3 :
With FTPClient and Total Commander, I get in both case a 4686 bytes file and I can open it without problem.

我的代码:

FTPClient ftpClient = new FTPClient();
ftpClient.connect("...", PORT);
ftpClient.login("...", "...");
ftpClient.enterLocalPassiveMode();
FTPFile[] imageFiles = ftpClient.listFiles(distantPathForImages);
for (FTPFile imageFile : imageFiles) {
    InputStream inputStream = ftpClient.retrieveFileStream(distantPathForImages + imageFile.getName());
    OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(new File(PATHDESTCSS + imageFile.getName())));
    byte[] bytesArray = new byte[65536];
    int bytesRead;
    while ((bytesRead = inputStream.read(bytesArray)) != -1) {
        outputStream.write(bytesArray, 0, bytesRead);
    }
    outputStream.close();
    inputStream.close();
    ftpClient.completePendingCommand();
}

为什么仅当我从服务器1下载文件时,我的文件才会具有这些额外字节"?

Why does my file have these "extra bytes" only when I download it from the server 1, and how can I fix this?

推荐答案

您的一台服务器可能尝试将文件作为文本传输,并且您的ftp客户端也认为它接收到文本.

One of your servers probably attempts to transmit the file as text, and your ftp client also thinks that it receives text.

这是javadoc的摘录:

Here is an excerpt from the javadoc:

如果当前文件类型为ASCII,则返回InputStream 将文件中的行分隔符转换为本地 表示形式.

If the current file type is ASCII, the returned InputStream will convert line separators in the file to the local representation.

如果您在Windows上,则每个换行符都会被'linebreak + cr'替换,这会对png文件中的所有数据结构造成严重破坏.

If you are on windows, every line break will be replaced by 'linebreak + cr', wreaking havoc on all data structures in the png file.

此方案的预期字节数为:4686 *(1 +1/256)= 4704.3046875,因为平均而言,png文件中的每个第256个字节应看起来像是ASCII换行符,因此将导致在一个额外增加的字节中.您的文件最终有4706个字节,非常接近.

The expected number of bytes for this scenario is: 4686 * (1 + 1 / 256) = 4704.3046875 , because on average, every 256-th byte in a png file should look like an ASCII line break, and will therefore result in an extra added byte. Your file ends up having 4706 bytes, which is pretty close.

将文件类型设置为FTP.BINARY_FILE_TYPE应该可以解决此问题:

Setting file type to FTP.BINARY_FILE_TYPE should fix this: https://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPClient.html#setFileType(int)

这篇关于从“某"下载的图像FTPClient的服务器已损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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