没有合适的资源?快使用搜索试试~ 我知道了~
Jsoup一款Java版数据挖掘工具,该资源包提供一键下载网络资源到本地 只需一个url 可携带cookie 携带自定义请求头 模拟用户操作
资源推荐
资源详情
资源评论
public static void main(String[] args) throws IOException {
//Open a URL Stream
Connection.Response resultImageResponse = Jsoup.connect("目标地址").ignoreContentType(true).execute();
// 解决url中可能有中文情况
URL url = new URL("目标地址");
HttpURLConnection http = (HttpURLConnection)url.openConnection();
http.setConnectTimeout(3000);
// 设置 User-Agent 避免被拦截
http.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)");
如有Cookie在此处添加
String contentType = http.getContentType();
System.out.println("contentType: "+ contentType);
// 获取文件大小
long length = http.getContentLengthLong();
System.out.println("文件大小:"+(length / 1024)+"KB");
// 获取文件名
InputStream inputStream = http.getInputStream();
byte[] buff = new byte[1024*10];
File file=new File("D:/","zi.zip");
OutputStream out = new FileOutputStream(file);
int len ;
int count = 0; // 计数
while((len = inputStream.read(buff)) != -1) {
out.write(buff, 0, len);
out.flush();
++count ;
}
System.out.println("count:"+ count);
// 关闭资源
//Open a URL Stream
Connection.Response resultImageResponse = Jsoup.connect("目标地址").ignoreContentType(true).execute();
// 解决url中可能有中文情况
URL url = new URL("目标地址");
HttpURLConnection http = (HttpURLConnection)url.openConnection();
http.setConnectTimeout(3000);
// 设置 User-Agent 避免被拦截
http.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)");
如有Cookie在此处添加
String contentType = http.getContentType();
System.out.println("contentType: "+ contentType);
// 获取文件大小
long length = http.getContentLengthLong();
System.out.println("文件大小:"+(length / 1024)+"KB");
// 获取文件名
InputStream inputStream = http.getInputStream();
byte[] buff = new byte[1024*10];
File file=new File("D:/","zi.zip");
OutputStream out = new FileOutputStream(file);
int len ;
int count = 0; // 计数
while((len = inputStream.read(buff)) != -1) {
out.write(buff, 0, len);
out.flush();
++count ;
}
System.out.println("count:"+ count);
// 关闭资源
资源评论
张小岛
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功