Capture server image download to local, capture server image download

Source: Internet
Author: User

Capture server image download to local, capture server image download
Sample code: import org. apache. http. header; import org. apache. http. httpResponse; import org. apache. http. httpStatus; import org. apache. http. client. clientProtocolException; import org. apache. http. client. httpClient; import org. apache. http. client. methods. httpGet; import org. apache. http. impl. client. defaultHttpClient; public void download (String path, String url) {// String url = "xxxxxxxx"; // String path = "F:/test.jpg"; HttpClient client = null; try {// create the HttpClient object client = new DefaultHttpClient (); // obtain the HttpGet object HttpGet httpGet = getHttpGet (url, null, null); // send the request to obtain the returned result HttpResponse response = client.exe cute (httpGet); // if (response. getStatusLine (). getStatusCode () = HttpStatus. SC _ OK) {byte [] result = EntityUtils. toByteArray (response. getEntity (); BufferedOutputStream bw = null; try {// Create a File object File f = new File (path); // create a File path if (! F. getParentFile (). exists () f. getParentFile (). mkdirs (); // Write File bw = new BufferedOutputStream (new FileOutputStream (path); bw. write (result);} catch (Exception e) {log. error ("Saving file error, path =" + path + ", url =" + url, e);} finally {try {if (bw! = Null) bw. close ();} catch (Exception e) {log. error ("finally BufferedOutputStream shutdown close", e) ;}}// if the else {StringBuffer errorMsg = new StringBuffer (); errorMsg. append ("httpStatus:"); errorMsg. append (response. getStatusLine (). getStatusCode (); errorMsg. append (response. getStatusLine (). getReasonPhrase (); errorMsg. append (", Header:"); Header [] headers = response. getAllHeaders (); fo R (Header: headers) {errorMsg. append (header. getName (); errorMsg. append (":"); errorMsg. append (header. getValue ();} log. error ("HttpResonse Error:" + errorMsg) ;}} catch (ClientProtocolException e) {log. error ("the downloaded file is saved locally. An http connection exception occurs. path =" + path + ", url =" + url, e);} catch (IOException e) {log. error ("the downloaded file is saved locally. The file operation is abnormal. path =" + path + ", url =" + url, e) ;}finally {try {client. getConnectio NManager (). shutdown ();} catch (Exception e) {log. error ("finally HttpClient shutdown error", e) ;}} private static HttpGet getHttpGet (String url, Map <String, String> params, String encode) {StringBuffer buf = new StringBuffer (url); if (params! = Null) {// Add an address? Or & String flag = (url. indexOf ('? ') =-1 )? "? ":" & "; // Add the parameter for (String name: params. keySet () {buf. append (flag); buf. append (name); buf. append ("="); try {String param = params. get (name); if (param = null) {param = "";} buf. append (URLEncoder. encode (param, encode);} catch (UnsupportedEncodingException e) {log. error ("URLEncoder Error, encode =" + encode + ", param =" + params. get (name), e);} flag = "&" ;}} HttpGet httpGet = new HttpGet (buf. toString (); return httpGet ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.