1. Save to server
Save from the path to the server on which the project resides.
String imgurl= "";//Picture address
try {
//construct URL url url
= new URL (imgurl);
Open connection
urlconnection con = url.openconnection ();
Input stream
InputStream is = Con.getinputstream ();
1K data buffering
byte[] bs = new byte[1024];
The length of data read
int len;
Output file stream
outputstream os = new FileOutputStream ("c:\\image.jpg");//save path
//start read while (
len = Is.read ( BS))!=-1) {
os.write (BS, 0, Len);
}
Complete, close all links
os.close ();
Is.close ();
} catch (Malformedurlexception e) {
e.printstacktrace ();
} catch (FileNotFoundException e) {
E.printstacktrace ();
} catch (IOException e) {
e.printstacktrace ();
}
2. Save to Local
Saved to local as a browser download.
String Imgurl= "";//url address string fileName = imgurl.substring (Imgurl.lastindexof ('/') + 1);
Bufferedinputstream is = null;
Bufferedoutputstream OS = null;
try {URL url = new URL (imgurl);
This.getservletresponse (). setContentType ("application/x-msdownload;"); This.getservletresponse (). SetHeader ("Content-disposition", "attachment;
Filename= "+ New String (Filename.getbytes (" Utf-8 ")," iso8859-1 "));
This.getservletresponse (). SetHeader ("Content-length", String.valueof (Url.openconnection (). GetContentLength ()));
is = new Bufferedinputstream (Url.openstream ());
OS = new Bufferedoutputstream (This.getservletresponse (). Getoutputstream ());
byte[] buff = new byte[2048];
int bytesread;
while ( -1!= (bytesread = is.read (buff, 0, buff.length))) {os.write (buff, 0, bytesread);
} if (is!= null) is.close ();
if (OS!= null) os.close ();
catch (Malformedurlexception e) { E.printstacktrace ();
catch (Unsupportedencodingexception e) {e.printstacktrace ();
catch (IOException e) {e.printstacktrace (); }
The above is based on the URL to download pictures to the client, server simple example is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.