• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem downloading an image using JSP

 
Md Fizal
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • 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?
 
danny liu
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!