本文介绍了Java使用JakartaFtpWrapper上传jpg - 使文件不可读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用JakartaFtpWrapper将客户端Java应用程序中的文件上传到我的服务器(用于备份)。



上传的文件是文本文件, png文件和jpgs。



我注意到在本地机器上有效的jpg文件 - 在服务器上变得不可读(损坏的文件) FTPd)。
图像文件的大小与原始图像大小相似,但不知何故,它是有缺陷的。



下面是我用来将.jpg写入LOCAL的代码磁盘:

 public static void writeJpeg(BufferedImage bfImg,String fileName,float quality)throws IOException {
FileImageOutputStream output =空值;
try {
Iterator iter = ImageIO.getImageWritersByFormatName(jpeg);
ImageWriter writer =(ImageWriter)iter.next();
ImageWriteParam iwp = writer.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(quality); // 0到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 File imageFile = imageFiles [j]; (imageFile!= null&& imageFile.isFile()&&(FileUtils.getFileSuffix(imageFile).equals(jpg)|| FileUtils.getFileSuffix(imageFile).equals(png ))){//只上传图片文件
ftpClient.uploadFile(imageFile.getAbsolutePath(),imageFile.getName());


$ / code $ / pre
$ b $ p感谢
Ran

解决方案

服务器上运行的是什么?它是一个开箱即用的FTP服务器还是你写的东西?



图像是二进制数据。如果JakartaFtpWrapper提供了将FTP传输设置为二进制模式的选项,您应该这样做;我认为您的问题最可能的原因是在文本模式下处理传输的错误默认尝试。如果按字节比较小图像,则应该在 0x0a旁边添加或删除Carriage Returns((char) 0x0d ==(char)13) 的。如果是这样,那是你的问题。


I've been using JakartaFtpWrapper to upload files from the client Java application to my server (for backup purposes).

The files that are uploaded are text files, png files and jpgs.

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.

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();
    }
}

The ftp code is straight forward:

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());
        }
    }

Thanks,Ran

解决方案

What's running on the server? Is it an "out of the box" FTP server or something you wrote?

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 - 使文件不可读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 08:58