It's not a secret anymore!*
The moose likes Servlets and the fly likes Load image fromt disk to servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Load image fromt disk to servlet" Watch "Load image fromt disk to servlet" New topic
Author

Load image fromt disk to servlet

Diego Creador
Greenhorn

Joined: Jan 10, 2011
Posts: 24
Hello friends!

Im being defeating by a servlet I'm trying to load an image from my disk to this servlet. The problem is that when I run it, no image is display. Im not using a jsp or anything else to display the image, I just call the servlet

Here is the code


Thanks a lot for any advide, and sorry for my English
Regards
Diego
Sai Hegde
security forum advocate
Ranch Hand

Joined: Oct 26, 2010
Posts: 200
    
    1

Try setting the response content type as follows and check --

Diego Creador
Greenhorn

Joined: Jan 10, 2011
Posts: 24
Hello Sai!

thanks for the anwser, I tried that but still doesnt work. I debuged the servlet and it looks like the image is copy fine, could it be a problem with the flush?? or should I set the output in somewhere else?


The code with that changes is:


Diego Creador
Greenhorn

Joined: Jan 10, 2011
Posts: 24
By the way, sorry for put the code without spaces, is there a way to do it??
Sai Hegde
security forum advocate
Ranch Hand

Joined: Oct 26, 2010
Posts: 200
    
    1

use the code blocks to put your code in.... you can always go back and edit your messages
Diego Creador
Greenhorn

Joined: Jan 10, 2011
Posts: 24
Done! Now it looks like a decent code.

Could be the problem on the flush? or I should I set the output variable to the response??
Sai Hegde
security forum advocate
Ranch Hand

Joined: Oct 26, 2010
Posts: 200
    
    1

Try doing this...

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18708
    
    8

Why do you think an image should be displayed?

I mean, you didn't say you were sending this request from a browser. If you aren't sending the request from a browser then it's the responsibility of whatever receives the response from that servlet to display it.

Or if you are using a browser, you didn't show us the HTML which submits the request. Perhaps the HTML isn't right.
Diego Creador
Greenhorn

Joined: Jan 10, 2011
Posts: 24
Appp maybe that could be the problem, here I copy that

My web. xml is this



And my jps file is call image.jsp



Sai Hegde
security forum advocate
Ranch Hand

Joined: Oct 26, 2010
Posts: 200
    
    1

You said, you were just hitting the servlet... Why do you need that extra jsp for?
Diego Creador
Greenhorn

Joined: Jan 10, 2011
Posts: 24
I really dont need it, but I created it to see if that was the problem. My originally idea is just to hit the servlet
Sai Hegde
security forum advocate
Ranch Hand

Joined: Oct 26, 2010
Posts: 200
    
    1

Yeah, all you need is the servlet.

Map your servlets the correct way in the web.xml...




Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18708
    
    8

Looks like you're confused in the usual way for beginners. You're confused between the code which sends the request and the code which processes the request. (I found that hard to keep straight when I started web programming too.) That JSP you posted doesn't need to be a JSP (like Sai Hegde said). It just needs to be an HTML which sends the request to the servlet.

The HTML part of what you have there looks okay to me. The JSP part, where you declared that the JSP produces a JPEG:



That's the confused part. That JSP doesn't produce an image at all. It produces some HTML which contains an image. It happens that the image that it contains is returned by a servlet, but the HTML doesn't care about that. So when you're testing this, just write a simple piece of HTML which includes the <img> element.
Diego Creador
Greenhorn

Joined: Jan 10, 2011
Posts: 24
I modified the web.xml file, but still when I hit the server it cannot display the image. It appear the red X, like the image is not avaible
Sai Hegde
security forum advocate
Ranch Hand

Joined: Oct 26, 2010
Posts: 200
    
    1

The fact that you see a red cross means that the image is not being found at the location. Please try to put the image in your project root and test your servlet.
Diego Creador
Greenhorn

Joined: Jan 10, 2011
Posts: 24
I moved the image to the proyect, created a folder in this path WebContext/imagnes and there drop the image

I change the line thar read the file input stream to this



But It cannot find the file, the method getRealPath gives the rigth location of the jpg but FileInputStream dont find it
Diego Creador
Greenhorn

Joined: Jan 10, 2011
Posts: 24
Hey!
I found out what was the problem, I run this servlet on firefox and works fine. The problem is the IE.

Sai Hegde
security forum advocate
Ranch Hand

Joined: Oct 26, 2010
Posts: 200
    
    1


I don't think its a browser specific problem. Firefox really doesnt care what kind of image it is once the response content type is set to a image. Can you post the html source for the rendered page in IE.
Diego Creador
Greenhorn

Joined: Jan 10, 2011
Posts: 24
I dont use an html page. I hit the server from the browser usiing http://localhost:8080/ImageExtractor/ImageExtractor
from Firefox works fine, but from IE no image es display (red X like if it missing)
Sai Hegde
security forum advocate
Ranch Hand

Joined: Oct 26, 2010
Posts: 200
    
    1

Hey Diego,

What was rendered is nothing but an HTML page. Just try running it against IE, right click to View Source and paste it in here.

Thanks,
Sai
Diego Creador
Greenhorn

Joined: Jan 10, 2011
Posts: 24
I canĀ“t because the source code option is gray, like it is not avaible. Both in IE and Firefox
Sai Hegde
security forum advocate
Ranch Hand

Joined: Oct 26, 2010
Posts: 200
    
    1

Check this for a reference and maybe you can point what was it that was incorrect -

http://www.coderanch.com/how-to/java/ImageServlet
Diego Creador
Greenhorn

Joined: Jan 10, 2011
Posts: 24
Sai, I found out that the problem could be the jgp flie.

I dragged and dropped on IE and firefox the jpg file , on IE the file couldnt be display on firefox is displayed fine. Perhaps is because this jpg file is a subimage of a larger .tiff file and the IE cant support this?

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Load image fromt disk to servlet