Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 1.30 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. java code to download an image from server to client
  2. import java.awt.Image;
  3. import java.io.BufferedInputStream;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9.  
  10.  
  11. import java.net.HttpURLConnection;
  12. import java.net.MalformedURLException;
  13. import java.net.URL;
  14. import javax.imageio.ImageIO;
  15.  
  16.  
  17.  
  18.  
  19. public class DownloadingImages{
  20.     public DownloadingImages() {}
  21.  
  22. public void download(String name) throws MalformedURLException, IOException{
  23.  
  24. Image image = null;
  25. try {
  26.     //URL url = new URL("file:///E:/myproject/build/web/images/Webcam.jpg");
  27.  
  28.  String  spath="http://localhost:5051/marketpoint/images/";
  29.  
  30.  String cpath="C:\";
  31.  
  32.  
  33.  spath = spath + name ;
  34.  cpath = cpath + name ;
  35.  System.out.println("FULL path::: "+spath);
  36.  
  37.  
  38.  
  39.  URL url = new URL(spath);
  40.  
  41.  
  42.  
  43.  
  44.  InputStream in = new BufferedInputStream(url.openStream());
  45. ByteArrayOutputStream out = new ByteArrayOutputStream();
  46. byte[] buf = new byte[1024];
  47. int n = 0;
  48. while (-1!=(n=in.read(buf)))
  49. {
  50.    out.write(buf, 0, n);
  51. }
  52. out.close();
  53. in.close();
  54. byte[] response = out.toByteArray();
  55.  FileOutputStream fos = new FileOutputStream(cpath);
  56.     fos.write(response);
  57.     fos.close();  
  58. } catch (IOException e) {
  59.  
  60.  
  61. }
  62. }
  63. }
  64.  
  65.  
  66. Here
  67. name = name of image thta client wants to download.