java httpclient 下载_HttpClient实现通过url下载文件

packagemain.java.com.abp.util;import org.apache.http.*;importorg.apache.http.client.HttpClient;importorg.apache.http.client.methods.HttpGet;importorg.apache.http.impl.client.DefaultHttpClient;importorg.apache.http.impl.client.HttpClients;importjava.io.File;importjava.io.FileOutputStream;importjava.io.InputStream;public classHttpDownload {public static final int cache = 10 * 1024;public static final booleanisWindows;public static finalString splash;public static finalString root;static{if (System.getProperty("os.name") != null && System.getProperty("os.name").toLowerCase().contains("windows")) {

isWindows= true;

splash= "\\";

root= "D:";

}else{

isWindows= false;

splash= "/";

root= "/search";

}

}/*** 根据url下载文件,文件名从response header头中获取

*

*@paramurl

*@return

*/

public staticString download(String url) {return download(url, null);

}/*** 根据url下载文件,保存到filepath中

*

*@paramurl

*@paramfilepath

*@return

*/

public staticString download(String url, String filepath) {try{

HttpClient client=HttpClients.createDefault();

HttpGet httpget= newHttpGet(url);

HttpResponse response=client.execute(httpget);

HttpEntity entity=response.getEntity();

InputStream is=entity.getContent();if (filepath == null){

filepath=getFilePath(response);

}

File file= newFile(filepath);

file.getParentFile().mkdirs();

FileOutputStream fileout= newFileOutputStream(file);/*** 根据实际运行效果 设置缓冲区大小*/

byte[] buffer = new byte[cache];int ch = 0;while ((ch = is.read(buffer)) != -1) {

fileout.write(buffer,0, ch);

}

is.close();

fileout.flush();

fileout.close();

}catch(Exception e) {

e.printStackTrace();

}return null;

}/*** 获取response要下载的文件的默认路径

*

*@paramresponse

*@return

*/

public staticString getFilePath(HttpResponse response) {

String filepath= root +splash;

String filename=getFileName(response);if (filename != null) {

filepath+=filename;

}else{

filepath+=getRandomFileName();

}returnfilepath;

}/*** 获取response header中Content-Disposition中的filename值

*

*@paramresponse

*@return

*/

public staticString getFileName(HttpResponse response) {

Header contentHeader= response.getFirstHeader("Content-Disposition");

String filename= null;if (contentHeader != null) {

HeaderElement[] values=contentHeader.getElements();if (values.length == 1) {

NameValuePair param= values[0].getParameterByName("filename");if (param != null) {try{//filename = new String(param.getValue().toString().getBytes(), "utf-8");//filename=URLDecoder.decode(param.getValue(),"utf-8");

filename =param.getValue();

}catch(Exception e) {

e.printStackTrace();

}

}

}

}returnfilename;

}/*** 获取随机文件名

*

*@return

*/

public staticString getRandomFileName() {returnString.valueOf(System.currentTimeMillis());

}public static voidoutHeaders(HttpResponse response) {

Header[] headers=response.getAllHeaders();for (int i = 0; i < headers.length; i++) {

System.out.println(headers[i]);

}

}public static voidmain(String[] args) {

String url= "https://codeload.github.com/douban/douban-client/legacy.zip/master";

String filepath= "F:\\xxxx\\workspace\\httpClientProj\\file\\test.zip";

HttpDownload.download(url, filepath);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值