package com.swb.utils;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Jsoup;
public class JsoupGetFile {
public static void main(String[] args) throws IOException {
// 请求地址
String imageUrl = "http://images.mofcom.gov.cn/trb/202203/20220301163904512.pdf";
Connection connect = Jsoup.connect(imageUrl);
Connection.Response response = connect.maxBodySize(5000000).method(Method.GET).ignoreContentType(true).execute();
System.out.println("文件类型为:" + response.contentType());
byte[] bufferedInputStream = response.bodyAsBytes();
// 保存文件到指定目录下
saveFile(bufferedInputStream, "C:\\Users\\10496\\Desktop\\文档\\photo\\1.pdf");
//byteToFile(bufferedInputStream,"C:\\Users\\10496\\Desktop\\文档\\photo\\","1.pdf");
}
/**
* saveFile 保存文件操作
* @param in 字节数组
* @param savePath 存储路径
* @throws IOException 异常处理
*/
static void saveFile(byte[] in, String savePath) throws IOException {
//byte[] buffer = new byte[1024];
//int len = 0;
// 创建缓冲流
FileOutputStream fileOutStream = new FileOutputStream(new File(savePath));
BufferedOutputStream bufferedOut = new BufferedOutputStream(fileOutStream);
// 图片写入
bufferedOut.write(in, 0, in.length);
// 缓冲流释放与关闭
bufferedOut.flush();
bufferedOut.close();
}
/**
* 字节转换文件
* @param bfile 字节数组
* @param filePath 文件夹路径
* @param fileName 文件名
*/
public static void byteToFile(byte[] bfile, String filePath,String fileName) {
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try {
File dir = new File(filePath);
if(!dir.exists()&&dir.isDirectory()){//判断文件目录是否存在
dir.mkdirs();
}
file = new File(filePath+File.separator+fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bfile);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
注意以下两点:
1,看到有的博文有一个response.bodyStream()方法,我尝试过但貌似行不通,所以就只能直接用数组转换成文件了。
2,Jsoup默认下载文件大小是1M所以很多文件下载下来不完整,记得设置maxBodySize(5000000)