*
The moose likes Servlets and the fly likes how to download an jpeg and gif file from a servlet to browser    Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to download an jpeg and gif file from a servlet to browser    " Watch "how to download an jpeg and gif file from a servlet to browser    " New topic
Author

how to download an jpeg and gif file from a servlet to browser

Kannan TKannan
Greenhorn

Joined: Jul 20, 2006
Posts: 5
I'm using the following code.
I don't know what is wrong with this code?

OutputStream outStream=res.getOutputStream();
setContentType("image/gif"); OR setContentType("image/jpef");
res.setHeader("Content-Disposition", "attachment; filename="+ fileToBeDownloaded.getName());
res.setHeader("Content-Length", String.valueOf(fileToBeDownloaded.length()));
FleInputStream fis=new FileInputStream(fileToBeDownloaded);
byte b[]=new byte[(int)fileToBeDownloaded.length()];
fis.read(b);
outStream.write(b);
outStream.flush();
outStream.close();

The file is getting downloaded. But it is not opening .....

thanks in advance.
Kannan.T
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41621
    
  55
If you want the file to be opened by the browser, leave out the Content-Disposition header.

I'm assuming "image/jpef" is just a typo.


Ping & DNS - my free Android networking tools app
Kannan TKannan
Greenhorn

Joined: Jul 20, 2006
Posts: 5
Thanks for ur reply.
In my program it is res.setContentType("image/jpeg"); only.
Only typing error.

Pls give me some solution.
Kannan.T
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41621
    
  55
Did you try what I suggested? If it didn't work, describe in more detail what you are trying to achieve, and what is actually happening.
Kannan TKannan
Greenhorn

Joined: Jul 20, 2006
Posts: 5
I just want to download pdf or jpeg file from a servlet.
I'm using the above code to do that.
Pdf is downloading properly and on opening that downloaded pdf file it is openning properly in Adobe.
Similarly, while downloading jpeg file
[response.setContentType("image/jpeg");], the downloading is proper.
I mean the same no of bytes downloads properly.
But on openning that downloaded jpeg file a red colored X mark is comming on top left cornor of the browser. I'm unable to open the downloaded jpeg file.
Please give me some solution.

Kannan.T
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If you want a working example of this, go to http://simple.souther.us
and look for SimpleStream.

You can just download the war file and run it.
The source code is in there.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Kannan TKannan
Greenhorn

Joined: Jul 20, 2006
Posts: 5
I searched thro the given link but i didn't find anything regarding this issue. There some HelloServlet.java, HelloJSP.jsp .....simple examples only they have given.

Pls give me some solution
Kannan.T
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to download an jpeg and gif file from a servlet to browser