posts - 710,  comments - 81,  views - 260万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
复制代码
 1 public HttpServletResponse download(String path, HttpServletResponse response) {
 2         try {
 3             // path是指欲下载的文件的路径。
 4             File file = new File(path);
 5             // 取得文件名。
 6             String filename = file.getName();
 7             // 取得文件的后缀名。
 8             String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
 9 
10             // 以流的形式下载文件。
11             InputStream fis = new BufferedInputStream(new FileInputStream(path));
12             byte[] buffer = new byte[fis.available()];
13             fis.read(buffer);
14             fis.close();
15             // 清空response
16             response.reset();
17             // 设置response的Header
18             response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
19             response.addHeader("Content-Length", "" + file.length());
20             OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
21             response.setContentType("application/octet-stream");
22             toClient.write(buffer);
23             toClient.flush();
24             toClient.close();
25         } catch (IOException ex) {
26             ex.printStackTrace();
27         }
28         return response;
29     }
30 
31     public void downloadLocal(HttpServletResponse response) throws FileNotFoundException {
32         // 下载本地文件
33         String fileName = "Operator.doc".toString(); // 文件的默认保存名
34         // 读到流中
35         InputStream inStream = new FileInputStream("c:/Operator.doc");// 文件的存放路径
36         // 设置输出的格式
37         response.reset();
38         response.setContentType("bin");
39         response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
40         // 循环取出流中的数据
41         byte[] b = new byte[100];
42         int len;
43         try {
44             while ((len = inStream.read(b)) > 0)
45                 response.getOutputStream().write(b, 0, len);
46             inStream.close();
47         } catch (IOException e) {
48             e.printStackTrace();
49         }
50     }
51 
52     public void downloadNet(HttpServletResponse response) throws MalformedURLException {
53         // 下载网络文件
54         int bytesum = 0;
55         int byteread = 0;
56 
57         URL url = new URL("windine.blogdriver.com/logo.gif");
58 
59         try {
60             URLConnection conn = url.openConnection();
61             InputStream inStream = conn.getInputStream();
62             FileOutputStream fs = new FileOutputStream("c:/abc.gif");
63 
64             byte[] buffer = new byte[1204];
65             int length;
66             while ((byteread = inStream.read(buffer)) != -1) {
67                 bytesum += byteread;
68                 System.out.println(bytesum);
69                 fs.write(buffer, 0, byteread);
70             }
71         } catch (FileNotFoundException e) {
72             e.printStackTrace();
73         } catch (IOException e) {
74             e.printStackTrace();
75         }
76     }
复制代码

 程序员的基础教程:菜鸟程序员

posted on   itprobie-菜鸟程序员  阅读(295)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示