我一直在使用 JakartaFtpWrapper 将文件从客户端 Java 应用程序上传到我的服务器(用于备份目的).
I've been using JakartaFtpWrapper to upload files from the client Java application to my server (for backup purposes).
上传的文件有文本文件、png文件和jpgs.
The files that are uploaded are text files, png files and jpgs.
我注意到在本地机器上有效的 jpg 文件在服务器(它们被 FTP 传输到的位置)上不知何故变得不可读(损坏的文件).图像文件大小与原始文件大小相似,但不知何故存在缺陷.
I've noticed that the jpg files which are valid on the local machine - somehow become unreadable (corrupt files) on the server (where they were FTPd to). The image file size is similar to the original one, but somehow it is defected.
这是我用来将 jpg 写入本地磁盘的代码:
Here's a code I'm using to write the jpg to the LOCAL disk:
public static void writeJpeg(BufferedImage bfImg, String fileName, float quality) throws IOException{
FileImageOutputStream output = null;
try{
Iterator iter = ImageIO.getImageWritersByFormatName("jpeg");
ImageWriter writer = (ImageWriter)iter.next();
ImageWriteParam iwp = writer.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(quality); // an integer between 0 and 1
File file = new File(fileName);
output = new FileImageOutputStream(file);
writer.setOutput(output);
IIOImage image = new IIOImage(bfImg, null, null);
writer.write(null, image, iwp);
}
finally{
if (output != null){
output.close();
}
}
ftp 代码很简单:
JakartaFtpWrapper ftpClient = new JakartaFtpWrapper();
ftpClient.connectAndLogin(FTP_URL, FTP_USER, FTP_PASSWORD);
ftpClient.setPassiveMode(true);
File[] imageFiles = folder.listFiles()
for (int j=0; j<imageFiles.length; j++){
File imageFile = imageFiles[j];
if (imageFile != null && imageFile.isFile() && (FileUtils.getFileSuffix(imageFile).equals("jpg") || FileUtils.getFileSuffix(imageFile).equals("png"))){ // upload only image files
ftpClient.uploadFile(imageFile.getAbsolutePath(), imageFile.getName());
}
}
谢谢,然
服务器上正在运行什么?它是开箱即用"的 FTP 服务器还是您编写的?
What's running on the server? Is it an "out of the box" FTP server or something you wrote?
图像是二进制数据.如果 JakartaFtpWrapper 提供了一些将 FTP 传输设置为二进制模式的选项,那么您应该这样做;我认为您的问题最可能的原因是在文本模式下处理传输的默认尝试错误.如果您按字节比较小图像,您应该会看到在 0x0a
旁边添加或删除了回车 ((char) 0x0d
== (char) 13).如果是这样,那是你的问题.
Images are binary data. If JakartaFtpWrapper offers some option of putting the FTP transfer into binary mode, you should do that; I think the most likely cause of your problem is a bad default attempt to process the transfer in text mode. If you compare small images bytewise, you should see Carriage Returns ((char) 0x0d
== (char) 13) being added or removed next to 0x0a
's. If so, that's your problem.
这篇关于Java 使用 JakartaFtpWrapper 上传 jpg - 使文件不可读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!