Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Downloading an Image

 
Dilshan Edirisuriya
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I download an image file when I hit the submit button.
I uses the code in HFSJ(page 129) but it didn't work even the jar file which is mentioned in the book. I got the servlet class file mentioning it's url pattern.
Can some one help me to download an image
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it didn't work

What does this mean? What does happen? Are there any exceptions or entries in the log files? TellTheDetails.
 
Dilshan Edirisuriya
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
response.setContentType("application/jar");
ServletContext ctx=getServletContext();
Here is my code

InputStream in=ctx.getResourceAsStream("/a.jar");
int read=0;
byte[] bytes=new byte[10240000];

OutputStream out=response.getOutputStream();
while((read=in.read(bytes))!=-1){
out.write(bytes,0,read);
}
out.flush();
out.close();
 
Dilshan Edirisuriya
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Dilshan Edirisuriya
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry I don't know how to display my screen printout
For the above code it displays a messagebox saying unknown file type and it tries to download the servlet class file mentioning it's url pattern
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
application/jar is not the correct content type for an image. For a JPEG I believe it's image/jpeg. It's definitely not downloading the servlet class, but in fact the image. But since you haven't given it a name, it's substituting the URL for it.

But it seems that you are in fact downloading a jar file. There's a disconnect somewhere between the code and your mentioning that you want to download an image.
 
Dilshan Edirisuriya
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I want to download an image in my datadase;
First I try it with a jar file .
But for the Jar file I get the class file of the servlet
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But for the Jar file I get the class file of the servlet

No, you don't. You're getting the jar file, but since you don't assign a name to it, the browser defaults to the name of the servlet. You should add code like:

response.setHeader("Content-Disposition", "attachment; filename=\"a.jar\"");
[ September 14, 2006: Message edited by: Ulf Dittmer ]
 
Dilshan Edirisuriya
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ulf Dittmer,

Thanks alot for your code. It worked fine.

Bye....
 
Khaled Mahmoud
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Setting the content type to application/anything will cause the browser to download the file
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic