
Untitled
By: a guest on
May 2nd, 2012 | syntax:
None | size: 1.30 KB | hits: 14 | expires: Never
java code to download an image from server to client
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.