FTPClient 中 ftp 上的文件未正确保存

Files not saving correctly on ftp in FTPClient

我正在使用 FTPClient 在 ftp 服务器上上传文件。首先,我有一个 TempFile 对象(不管这个对象是什么),它有一个 getBytes() 方法。此方法 returns 字节列表:

List<Byte> byteList = tempFile.getBytes();
Byte[] byteObjects= new Byte[byteList.size()];
byte[] byteArray = new byte[byteList.size()];
byteObjects = byteList.toArray(byteObjects);
byteArray = ArrayUtils.toPrimitive(byteObjects);
InputStream stream = new ByteArrayInputStream(byteArray);
ftpClient.storeFile(file.getName()+"."+file.getExtension(), stream);

执行最后一行代码后,成功创建了一个具有预期名称的文件,但是当我想打开该文件时,我在浏览器中看到 "The image test.jpg can not be displayed, because it contains error"。有什么问题?

听起来您需要将 ftp 客户端设置为以二进制格式传输

ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

根据文档

If the current file type is ASCII, line separators in the file are transparently converted to the NETASCII format