title: java code to download an image from server to client
body: <p>I am a new to java. I dont know much about it. I am just learning java.
  I am developing a web application. In which i have an option download image . If user clicks he should be able to download image from server to client side say at location c://. </p>
  
  <p>I have implemented this code ::</p>
  
  <pre><code>import java.awt.Image;
  import java.io.BufferedInputStream;
  import java.io.ByteArrayOutputStream;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  
  
  import java.net.HttpURLConnection;
  import java.net.MalformedURLException;
  import java.net.URL;
  import javax.imageio.ImageIO;
  
  
  
  
  public class DownloadingImages{
      public DownloadingImages() {}
  
  public void download(String name) throws MalformedURLException, IOException{
  
  Image image = null;
  try {
      //URL url = new URL("file:///E:/myproject/build/web/images/Webcam.jpg");
  
   String  spath="http://localhost:5051/marketpoint/images/";
  
   String cpath="C:\\";
  
  
   spath = spath + name ;
   cpath = cpath + name ;
   System.out.println("FULL path::: "+spath);
  
  
  
   URL url = new URL(spath);
  
  
  
  
   InputStream in = new BufferedInputStream(url.openStream());
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  byte[] buf = new byte[1024];
  int n = 0;
  while (-1!=(n=in.read(buf)))
  {
     out.write(buf, 0, n);
  }
  out.close();
  in.close();
  byte[] response = out.toByteArray();
   FileOutputStream fos = new FileOutputStream(cpath);
      fos.write(response);
      fos.close();  
  } catch (IOException e) {
  
  
  }
  }
  }
  
  
  Here
  name = name of image thta client wants to download.
  </code></pre>
  
  <p>Here problem is that image gets downloaded to server's side. At c:// . can anybody please tell where  am i going wrong.</p>
  
  <p>For this i am using net beans as my editor , Apache tomcat as  server.  Both client and Server gets connected through port no 5051. And image that clients want to download from server is simple jpg image.  will anybody help me to get out of this problems. </p>