Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Kannan TKannan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kannan TKannan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kannan TKannan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic