Java tutorial
package buildhappy.tools; /** * ? * @author Administrator * */ import java.io.DataOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.BufferedInputStream; import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.params.HttpMethodParams; public class DownloadFile { /** * ?URL?????URL??? */ public String getFileNameByUrl(String url, String contentType) { //url = "http://www.sina.com.cn/license/map2011.html"; //http:// url = url.substring(7); //text/html if (contentType.indexOf("html") != -1) { url = url.replaceAll("[\\?/:*|<>\"]", "_") + ".html"; //System.out.println("???" + url); return url; } //application/pdf else { int beginIndex = contentType.lastIndexOf("/") + 1; url = url.replaceAll("[\\?/:*|<>\"]", "_") + "." + contentType.substring(beginIndex); //System.out.println("???" + url); return url; } } /** * ?filepath??? */ private void saveToLocal(byte[] data, String filePath) { FileOutputStream fileOut = null; DataOutputStream dataOut = null; try { fileOut = new FileOutputStream(new File(filePath)); dataOut = new DataOutputStream(fileOut); for (int i = 0; i < data.length; i++) { dataOut.write(data[i]); } dataOut.flush(); } catch (IOException e) { e.printStackTrace(); } finally { if (dataOut != null) { try { dataOut.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * URL */ public String downloadFile(String url) { String filePath = null; //1.?HttpClient?? HttpClient client = new HttpClient(); //5s client.getHttpConnectionManager().getParams().setConnectionTimeout(5000); //2.?GetMethod? GetMethod getMethod = new GetMethod(url); //get5s getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000); //?? getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); //3.http get try { int statusCode = client.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed:" + getMethod.getStatusLine()); filePath = null; } //4.?HTTP? InputStream in = getMethod.getResponseBodyAsStream(); BufferedInputStream buffIn = new BufferedInputStream(in); //byte[] responseBody = null; String responseBody = null; StringBuffer strBuff = new StringBuffer(); byte[] b = new byte[1024]; int readByte = 0; while ((readByte = buffIn.read(b)) != -1) { strBuff.append(new String(b)); } responseBody = strBuff.toString(); //buffIn.read(responseBody, off, len) //byte[] responseBody = getMethod.getResponseBody(); //?url???? filePath = "temp\\" + getFileNameByUrl(url, getMethod.getResponseHeader("Content-Type").getValue()); System.out.println(filePath + "--size:" + responseBody.length()); saveToLocal(responseBody.getBytes(), filePath); } catch (HttpException e) { System.out.println("check your http address"); e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // getMethod.releaseConnection(); } return filePath; } }