#下载网页资源
摘要:通过url下载各种网页资源,可以下载视频,图片,html等等文件
##可以下载资源
sudo su
package com;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
public class UrlDownload {
public static void main(String[] args) throws ParseException {
//所要下载的图片的url网址
String url = "https://nimg.ws.126.net/?url=http%3A%2F%2Fdingyue.ws.126.net%2F2021%2F1206%2F5ca940f9j00r3okp9001vc000hs00qoc.jpg&thumbnail=650x2147483647&quality=80&type=jpg";
//String url = "https://om.wangjm.ml/E5_File/WallPaper/ACGN/2021070717261775.jpg";
//图片下载的路径
String path = "E:\\picture\\xiaoshuo2\\md\\app.chunk.658e99ca.js";
//通过代理IP下载,有些图片不能下载
//getUriResource(url, path);
downloadPicture(url,path); //url下载,可以下载任何图片
}
/**
* 可以下载任何图片
* url.openStream()
* DataInputStream
* FileOutputStream
* ByteArrayOutputStream
*/
//链接url下载图片
@SuppressWarnings("unused")
private static void downloadPicture(String urlList,String path) {
URL url = null;
try {
url = new URL(urlList);
//URLConnection conn = url.openConnection();
//InputStream inStream = conn.getInputStream();
DataInputStream dataInputStream = new DataInputStream(url.openStream());
FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = dataInputStream.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
fileOutputStream.write(output.toByteArray());
dataInputStream.close();
// inStream.close();
fileOutputStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 通过代理ip下载,有些图片不能下载
*/
public static void getUriResource(String url, String path)throws ParseException {
//创建有代理的HttpClient
CloseableHttpClient httpclient;
//创建代理类对象
ProxyConfig proxyconfig = new ProxyConfig();
//获取代理
HttpHost proxy = proxyconfig.get_proxy();
//代理配置代理ip和端口
RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
//代理配置账号和密码
CredentialsProvider provider = proxyconfig.get_provider();
/*
* 配置代理账号和密码
*/
//HttpClient请求配置代理账号和密码
httpclient = HttpClients.custom().setDefaultCredentialsProvider(provider).build();
// 获取连接客户端工具
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
try {
// 创建get请求对象
HttpGet httpGet = new HttpGet(url);
/*
* 配置代理ip
*/
httpGet.setConfig(config);
// 浏览器表示
//httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)");
// 传输的类型
//httpGet.addHeader("Content-Type", "application/x-www-form-urlencoded");
// 执行请求
response = httpClient.execute(httpGet);
// 获得响应的实体对象
HttpEntity entity = response.getEntity();
/*
* 获取到响应信息的流
*/
InputStream is = entity.getContent();
// 包装成高效流
BufferedInputStream bis = new BufferedInputStream(is);
// 写入本地 D 盘
File file = new File(path);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
byte[] byt = new byte[1024];
Integer len = -1;
//int len =0;
while ((len = bis.read(byt)) != -1) {
bos.write(byt, 0, len);
}
bos.close();
bis.close();
} catch (ClientProtocolException e) {
System.err.println("Http协议出现问题");
e.printStackTrace();
} catch (IOException e) {
System.err.println("IO异常");
e.printStackTrace();
} finally {
// 释放连接
if (null != response) {
try {
response.close();
httpClient.close();
} catch (IOException e) {
System.err.println("释放连接出错");
e.printStackTrace();
}
}
}
}
}