*
The moose likes JSP and the fly likes Download image(jpg format) using URL in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Download image(jpg format) using URL in jsp" Watch "Download image(jpg format) using URL in jsp" New topic
Author

Download image(jpg format) using URL in jsp

velgapudi suresh
Ranch Hand

Joined: Aug 11, 2006
Posts: 32
Hi Every one,

In my application image download functionality is there. One beside of image we have a button to download the image using url functionality.


How we can do it, i tried it but image storing it 0 size.

anyone help out in this problem.



Regards,
Suresh.V

Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
We're going to need a lot more information about what, exactly, you tried, and how, exactly, that didn't work.
velgapudi suresh
Ranch Hand

Joined: Aug 11, 2006
Posts: 32
Hi Moores,

Thank you for the reply.

I tried below code


String imgLen = "D://sahi.JPG";

String filename = "image005.jpg";

int len = imgLen.length();
byte [] rb = new byte[len];
InputStream readImg = new BufferedInputStream(
new FileInputStream(imgLen));
int index=readImg.read(rb, 0, len);
//st1.close();
response.reset();
response.setContentType("image/JPG");
response.setHeader("Content-disposition","attachment; filename=" +filename);
response.getOutputStream().write(rb,0,len);
response.getOutputStream().flush();


this was done in my jsp scrip-let code. When i run the page i am able to get save window with the filename. But the content not able to read .

Where i was done mistake and where i can change my code.

please help out me.



Suresh.V
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

You should be doing this in a servlet. JSPs are meant as templates for text content, not images or other binary formats. Please read this article to understand what JSP is and why your approach is folly.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Download image(jpg format) using URL in jsp
 
Similar Threads
Regarding Upload a file
How to enable downloads
General Question Regarding Image Processing
Upload/Download of Files In Java
how we can create xls file in java