1

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

Ranch
  • 2,601
  • 4
  • 29
  • 28
  • May I ask why you're using the Java 2D API to handle the uploaded images? Do you want to modify the uploaded images before storing on disk? The code isn't doing anything interesting with the images. If one uploads an image, you normally should have got it in flavor of an `InputStream`. Normally you can just write it directly to some `OutputStream`, such as `FileOutputStream`. You don't need the Java 2D stuff for this. – BalusC Nov 28 '09 at 03:47
  • The way that code was arranged confused me too: The top section shows how the file is encoded (which we don't really care about) while the bottom part shows how it's uploaded. – Carl Smotricz Nov 28 '09 at 18:01
  • You are right, it is not relevant. But when posting this question I had a suspicion that the way the jpeg is encoded might be related to it. – Ranch Dec 02 '09 at 22:13

1 Answers1

1

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.

Carl Smotricz
  • 66,391
  • 18
  • 125
  • 167
  • This is it, thanks ! I should have used the .binary() method on JakartaFtpWrapper. I've managed also to rescue the jpg files using Hex editor, and that was a life saver ! – Ranch Nov 28 '09 at 17:50
  • Excellent, glad I could help! Please help make me rich and famous by upvoting my answer and accepting it! – Carl Smotricz Nov 28 '09 at 18:00