JAVA实现网络文件下载
2024-09-19 05:57:14
HttpURLConnection conn = null;
OutputStream outputStream = null;
InputStream inputStream = null;
try {
String path = r.getParameter("filename");
File fl = new File(path); URL url = new URL(path);
conn = (HttpURLConnection)url.openConnection();
inputStream = conn.getInputStream(); rp.reset();
rp.setHeader("Content-Disposition", "attachment;filename=\""+ fl.getName() + "\"");
rp.setContentType("application/octet-stream");
rp.setContentLength((int)conn.getContentLength());
rp.setHeader("Pragma", "No-cache");
rp.setHeader("Cache-Control", "No-cache");
rp.setDateHeader("Expires", 0); outputStream = new BufferedOutputStream(rp.getOutputStream());
byte[] buffer = new byte[4096];
int len = 0;
while((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
outputStream.flush();
} catch(Exception e){
e.printStackTrace();
} finally {
if(conn!=null){
conn.disconnect();
}
if(inputStream != null) {
inputStream.close();
}
if(outputStream != null) {
outputStream.close();
}
}
最新文章
- 利用poi导出Excel
- 大毕设-MATLAB-滤波器的实现
- SQL存储过程、视图
- js 添加广告
- java获取每个月的最后一天
- App引导页面源代码的实现
- BCS--使用SharePoint Designer创建外部内容类型
- google面试题,男孩男女比例?
- 关于Response.redirect和Response.End出现线程中止异常的处理
- log4j 日志脱敏处理 + java properties文件加载
- lambda高级进阶--组合函数
- C# WebSocket模拟发送接收
- 菜鸟学Java(二十三)——Java内存分析
- 代码编辑器之sublime text
- Set ARITHABORT Option设置为ON
- httpclient httpclient使用连接池
- Ubuntu 将应用程序固定到快速启动栏
- LoadRunner性能测试基础知识问答
- 快速上手Runtime(三)之方法交换
- 我的Android进阶之旅------>关于android:layout_weight属性的详细解析