問(wèn)題描述
我一直在使用 JakartaFtpWrapper 將文件從客戶端 Java 應(yīng)用程序上傳到我的服務(wù)器(用于備份目的).
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.
我注意到在本地機(jī)器上有效的 jpg 文件在服務(wù)器(它們被 FTP 傳輸?shù)降奈恢?上不知何故變得不可讀(損壞的文件).圖像文件大小與原始文件大小相似,但不知何故存在缺陷.
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.
這是我用來(lái)將 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 代碼很簡(jiǎn)單:
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());
}
}
謝謝,然
推薦答案
服務(wù)器上正在運(yùn)行什么?它是開(kāi)箱即用"的 FTP 服務(wù)器還是您編寫的?
What's running on the server? Is it an "out of the box" FTP server or something you wrote?
圖像是二進(jìn)制數(shù)據(jù).如果 JakartaFtpWrapper 提供了一些將 FTP 傳輸設(shè)置為二進(jìn)制模式的選項(xiàng),那么您應(yīng)該這樣做;我認(rèn)為您的問(wèn)題最可能的原因是在文本模式下處理傳輸?shù)哪J(rèn)嘗試錯(cuò)誤.如果您按字節(jié)比較小圖像,您應(yīng)該會(huì)看到在 0x0a
旁邊添加或刪除了回車 ((char) 0x0d
== (char) 13).如果是這樣,那是你的問(wèn)題.
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.
這篇關(guān)于Java 使用 JakartaFtpWrapper 上傳 jpg - 使文件不可讀的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!