Help coderanch get a
new server
by contributing to the fundraiser
  • 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

Problem downloading an image using JSP

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I'm trying to download an image file from internet using the following JSP code. It's not working and not throwing any error either. Could anyone help figure out the error?
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I never done this before. But I 've read a sample.
In your code, you open a Http connection, then you are waiting there.
You should send a GET request and then wait for the image response.
void saveFile(String strFileName){ try{ URL url = new URL(strImageDownloadPath + strFileName); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); String HTTP_PROXY_USERNAME = System.getProperty("http.proxyUsername"); String HTTP_PROXY_PASSWORD = System.getProperty("http.proxyPassword"); String authString = HTTP_PROXY_USERNAME + ":" + HTTP_PROXY_PASSWORD; String auth = "Basic " + new sun.misc.BASE64Encoder().encode( authString.getBytes() ); connection.setRequestProperty( "Proxy-Authorization", auth ); connection.setDoInput( true );
-- here add some code to send a request
InputStream is =
connection.getInputStream(); objFile = new FileOutputStream(strImageDownloadPath + strFileName); int b; while((b=is.read())!=-1){ objFile.write(b); } is.close(); objFile.close(); }catch(FileNotFoundException fnfe){ System.out.println("FileNotFoundException occured!!!"); }catch(IOException ioe){ }finally{ } }
also, at the server side, you should add the following code in jsp:
response.setContentType("image/jpeg")

Hope it helps.
 
You save more money with a clothesline than dozens of light bulb purchases. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic