• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Downloading an Image

 
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43026
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Dilshan Edirisuriya
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43026
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43026
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ulf Dittmer,

Thanks alot for your code. It worked fine.

Bye....
 
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Setting the content type to application/anything will cause the browser to download the file
 
Do not threaten THIS beaver! Not even with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic