• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Download image from servlet

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am working in site where i want to show advertisement images. I was planned to show that by using servlet.

I made following code. but its taking so mach time to download images.

protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

resp.setHeader("Content-length", "" + 900000);
resp.setContentType("image/jpg");
resp.setHeader("Content-Disposition","attachment; filename=\"ads.jpg\"");

/* FileInputStream inputStream = null;

File file = new File("http://localhost:8080/Test/images/ad1.jpg");
*/InputStream inp = null;
try
{
ServletContext context = getServletContext();

inp = context.getResourceAsStream("/images/Basketball.png");
/* InputStreamReader isr = new InputStreamReader(inp);
BufferedReader reader = new BufferedReader(isr);*/
byte[] buffer = new byte[90000];
int bytesRead =0;// buffer.length;
int i = 0;
do
{
bytesRead= inp.read(buffer);
System.out.println(" i: " + ++i + " byts Read " + bytesRead);

resp.getOutputStream().write(buffer,0,bytesRead);
} while (bytesRead == buffer.length);


resp.getOutputStream().flush();
System.out.println(" flush done");
}
finally
{
if(inp != null)
inp.close();

System.out.println(" finally complete ");
}

}



Please advice or give suggestion on this.


Thanks in advance...
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If this image is supposed to show in a HTML page, why are you sending as "attachment"

Does your code really say:

if so, why.

resp.setHeader("Content-length", "" + 900000);


Sending images works much better if you set the content-length to the actual file size.

Sending image bytes from a servlet should be quite fast - what time delay are you observing?

Bill
 
Ramesh Sengani
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks william,

Now its working fine.

I removed

resp.setHeader("Content-length", "" + 900000);


 
rubbery bacon. crispy tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic