
package com.example.climbnumber;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.zip.GZIPInputStream;
public class Test02 {
public static void main(String[] args) {
try {
download("https://disclosure.shcpe.com.cn//cpec-ent/file/20220508/1652002109174%E6%88%AA%E8%87%B32022%E5%B9%B44%E6%9C%8830%E6%97%A5%E6%8C%81%E7%BB%AD%E9%80%BE%E6%9C%9F%E5%90%8D%E5%8D%95.pdf", "C:\\Users\\13560\\Desktop\\12.pdf");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 下载文件到本地
* @param urlString
* @param filename
* @throws Exception
*/
public static void download(String urlString, String filename) throws Exception {
URL url = new URL(urlString);// 构造URL
URLConnection con = url.openConnection();// 打开连接
InputStream is = con.getInputStream();// 输入流
String code = con.getHeaderField("Content-Encoding");
if ((null != code) && code.equals("gzip")) {
GZIPInputStream gis = new GZIPInputStream(is);
// 1K的数据缓冲
byte[] bs = new byte[1024];
// 读取到的数据长度
int len;
// 输出的文件流
OutputStream os = new FileOutputStream(filename);
// 开始读取
while ((len = gis.read(bs)) != -1) {
os.write(bs, 0, len);
}
// 完毕,关闭所有链接
gis.close();
os.close();
is.close();
} else {
// 1K的数据缓冲
byte[] bs = new byte[1024];
// 读取到的数据长度
int len;
// 输出的文件流
OutputStream os = new FileOutputStream(filename);
// 开始读取
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
// 完毕,关闭所有链接
os.close();
is.close();
}
}
}

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~