Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Image download 2

Status
Not open for further replies.

Targol

Technical User
Sep 13, 2002
908
FR
Hi guys and girls !

I need to make a button on a HTML page in order to allow user to save (download) a given image to it's disk. The problem is that the image is located on a server that needs autentication. I managed to do it using the following method :
Code:
public void extractImage() {
  String urlParam = (String) params.get("URL"); // retrive img url
  try {
    urlParam = oper.getContextualUrl(urlParam); // adding authentication
    URL url = new URL(urlParam);
    InputStream is = url.openStream();
    ByteArrayOutputStream buff = new ByteArrayOutputStream();
    int b= is.read();
    while (b!=-1) {
      buff.write(b);
      b= is.read();
    }
    String contentType = "application/download";
    response.setContentType(contentType);
    OutputStream os = null;
    try {
      os = response.getOutputStream();
      os.write(buff.toByteArray());
    } catch (IOException e) {
      logger.error(e);
      displayException("Unable to write file on ouput",e);
    } finally {
      if(os != null) try{os.close();}catch(Exception silentErr){};
    }
  } catch (Exception e) {
    logger.error(e);
    displayException(e);
  }
}

The prob is that the response url looks like " so when the browser opens the "save as" dialog, the name given to the saved file is "FileItem.extractImage.wbx". Does anybody got an idea on :
[ul][li]How to retrieve mime type (and eventually the name) of the image ?[/li]
[li]Force the name (or just extension) of file name to save (by setting the response header for example) ?[/li]
[/ul]

Thanks for any clue...

Water is not bad as long as it remains outside human body ;-)
 
u have to add a "filename" to the header.

in ASP we do it likeso:

AddHeader "Content-Disposition", "filename=THENAME"


unfortunately i dont know how to do it in java...

Known is handfull, Unknown is worldfull
 
U're my god [lol]
That's exactly what I was looking for without manage to find it !!!

Water is not bad as long as it remains outside human body ;-)
 
In fact, the only prob is that it works perfectly with Firefox but with this solution, that F$^*#ing IE overrides the "application/download" mime type and opens the image in another window without opening save dialog. As IE is the second target for my appli, I need to support it as well. So if you can find a solution that works under IE, that'll be greatfull. I can pass to java the browser type to differenciate the 2 browsers if U find a solution for IE.

Water is not bad as long as it remains outside human body ;-)
 
try:
"application/octet-stream"

Known is handfull, Unknown is worldfull
 
Same thing : works fine for Firefox while IE opens the image into a new window....

Water is not bad as long as it remains outside human body ;-)
 
hmm, u have to give "attachment" word somewhere. let me check it up and come back...

Known is handfull, Unknown is worldfull
 
Hi,

Try this and give the filename

response.setContentType("image/jpeg");
response.setHeader("Content-disposition","attachment;filename=img.jpg");

Cheers
Venu
 
Thanks to you 2 : now that works under Firefox and IE !!!

Water is not bad as long as it remains outside human body ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top