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 ;}