Jill
Jill

Reputation: 69

What is the correct path for Toolkit.getImage()?

I need to upload an image file and generate a thumbnail for the uploaded file in my JSF webapplication. The original image is stored on the server in /home/myname/tomcat/webapps/uploads, while the thumbnail is stored in /home/myname/tomcat/webapps/uploads/thumbs. I'm using the thumbnail generator class I copied from philreeve.com.

I have successfully uploaded the file with help from BalusC. But using Toolkit.getImage(), I can't access the image.

I used the uploaded file's absolute path, like so:

inFilename = file.getAbsolutePath();

The relevant code from the thumbnail generator is:

public static String createThumbnail(String inFilename, String outFilename, int largestDimension) {
    ...
    Image inImage = Toolkit.getDefaultToolkit().getImage(inFilename);
    if (inImage.getWidth(null) == -1 || inImage.getHeight(null) == -1) {
        return "Error loading file: \"" + new File(inFilename).getAbsolutePath() + "\"";
    }
    ...
}

Since I am already using the absolute path, I don't understand why it is not working. I have also used the following values for inFilename, but I always get the "Error loading file...".

But I did check the directory, and the image is there. (I uploaded using /home/myname/tomcat/webapps/uploads/filename.ext, and it works.) What is the correct path for the image in that directory? Thank you.

Update

I got the code to work by using:

Image inImage = ImageIO.read(new File(inFilename));

I still don't understand why Toolkit.getImage() does not work though.

Upvotes: 3

Views: 13637

Answers (2)

ceperman
ceperman

Reputation: 415

The problem is that Toolkit.getImage() does not return the image immediately. The issue is well-described in this bug report, a relevant extract of which is here:

This is not a bug. The submitter is not properly using the asynchronous Image API correctly. He assumes that getImage loads all of the image's bits into memory. However, it is well documented that the actual loading of bits does not take place until a call to Component.prepareImage or Graphics.drawImage. In addition, these two functions return before the Image is fully loaded. Developers are required to install an ImageObserver to listen for notification that the Image has been fully loaded. Once they receive this notification, they can repaint the Image.

I found that the answer to this question works well:

Image image = new ImageIcon(this.getClass().getResource("/images/bell-icon16.png")).getImage();

Upvotes: 2

Aaron Digulla
Aaron Digulla

Reputation: 328614

Are you sure it's a JPEG file? Use an image viewer to make sure nothing bad happened to the file during upload (or that it was an image to begin with).

Also, use new File(inFilename).exists() to make sure the path is correct. I also suggest to print new File(inFilename).getAbsolutePath() in error messages because relative paths can hurt you.

That said, the rest of the code looks correct.

Upvotes: 2

Related Questions