要下载源代码java 版 获取css中的图片 httpClient4.1.3 下载
先看运行效果
这是选择文件 ,可以选择css和js
选择文件存放的路径
分析css中图片的url
填写网站的主路径很重要,下载图片就根据这个url和css中的url结合起来
这样就可以下载成功了。
部分 关键代码
public static String downLoad(HttpClient httpclient,String url,String savePath,String imgName) throws Exception, IOException{
HttpGet httpGet = new HttpGet(url);
HttpResponse response = httpclient.execute(httpGet);
if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println(entity.getContentType());
// 可以判断是否是文件数据流
System.out.println(entity.isStreaming());
// 设置本地保存的文件
//File storeFile = new File("F:/"+imgName);
File storeFile = new File(savePath+"/"+imgName);
FileOutputStream output = new FileOutputStream(storeFile);
// 得到网络资源并写入文件
InputStream input = entity.getContent();
byte b[] = new byte[1024];
int j = 0;
while ((j = input.read(b)) != -1) {
output.write(b, 0, j);
}
output.flush();
output.close();
}
if (entity != null) {
entity.consumeContent();
}
return "成功下载 "+url;
}
else {
httpGet.abort();
return "失败下载 "+url;
}
}