Get Image

Dear Experts,

           I have a java class. This class will get the image. It is running well and no exception thrown. But, when I deploy to the linux server, and put it in the tomcat, it still can run, but, when come to period of time, this class will stop before the code below and there is no exception thrown:

            Image image = Toolkit.getDefaultToolkit().getImage(inFile);

          Once I restart my tomcat, this class can still run without error. Can someone tell me what is the problem? Any idea to overcome this problem?

          This is part of my code:

            Image image = Toolkit.getDefaultToolkit().getImage(inFile);
            MediaTracker mediaTracker = new MediaTracker(new Frame());
            mediaTracker.addImage(image, 0);
            mediaTracker.waitForID(0);
            BufferedImage sImage = new BufferedImage(width,
                  height, BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics2D = sImage.createGraphics();
            graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                  RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            graphics2D.drawImage(image, 0, 0, width, height, null);
            BufferedOutputStream out = new BufferedOutputStream(new
                  FileOutputStream(outFile));
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
            JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(sImage);
            quality = Math.max(0, Math.min(quality, 100));
            param.setQuality((float)quality / 100.0f, false);
            encoder.setJPEGEncodeParam(param);
            encoder.encode(sImage);


Regards,
  Jocelyn
jocelynyipAsked:
Who is Participating?
 
CEHJCommented:
I'm not sure what's causing this but instead of:

>>
Image image = Toolkit.getDefaultToolkit().getImage(inFile);
MediaTracker mediaTracker = new MediaTracker(new Frame());
mediaTracker.addImage(image, 0);
mediaTracker.waitForID(0);
>>

Try


Image image = new ImageIcon(inFile).getImage();
0
 
CEHJCommented:
>>this class will stop before the code below and there is no exception thrown:

Are you certain there's no exception? What is happening just prior to that code being called?
0
 
jocelynyipAuthor Commented:
Yes, I am sure.

For an example:

System.out.println("Start Get Image!!!");
Image image = Toolkit.getDefaultToolkit().getImage(inFile);
System.out.println("End Get Image!!!");

In the console, I just can see "Start Get Image", and the println just stop here and from the console, I can't see any exception thrown. So, I know that there is problem with this code --> Image image = Toolkit.getDefaultToolkit().getImage(inFile);
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
CEHJCommented:
>>In the console, I just can see "Start Get Image",

How can you see the console if you're running in Tomcat?

>>ut, when I deploy to the linux server, and put it in the tomcat, it still can run, but ...
0
 
CEHJCommented:
e.g. if that image file is outside the web context, your code will crash in a web container
0
 
jocelynyipAuthor Commented:
I can see it throught the log catalina.out.

My code can run for a period of time and over a period of time, the code will stop at the code Image image = Toolkit.getDefaultToolkit().getImage(inFile);

I have to restart tomcat, and my code is running. That means that everytime it down, I have to restart tomcat to make it running.
0
 
CEHJCommented:
OK, so sometimes is *does* render the image ok using Tomcat ..?
0
 
jocelynyipAuthor Commented:
yes
0
 
CEHJCommented:
OK - we can rule out security issues then. Is this image by any chance a large one?
0
 
jocelynyipAuthor Commented:
What u mean by rule out security issue? The image size is 176x134
0
 
CEHJCommented:
>> What u mean by rule out security issue?

Problems due to the security policy of your web container

>>The image size is 176x134

Well i meant the file size, but i take it from that it's not large ;-)
0
 
jocelynyipAuthor Commented:
The file is not big, ard 60kb, some is only 17kb
0
 
CEHJCommented:
>>In the console, I just can see "Start Get Image", and the println just stop here

Is this *always* the case?

I'll imagine the above says 'In the log file' - it's confusing ;-)
0
 
jocelynyipAuthor Commented:
sorry, is the log will show the println.
0
 
CEHJCommented:
But you didn't answer the question:

>>Is this *always* the case?
0
 
jocelynyipAuthor Commented:
Everyday has such problem. Quite frequent.
0
 
jocelynyipAuthor Commented:
ok, thank you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.