1 package com.lc.util;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.InputStream;
 6 import java.io.OutputStream;
 7 import javax.servlet.http.HttpServletResponse;
 8 import org.apache.struts2.ServletActionContext;
 9 
10 public class DownLoadUtil {
11     
12     /**
13      * 文件下载
14      * @param filePath 提供下载的文件(路径+文件名)
15      * @param showName 弹框显示的文件名
16      * @throws Exception
17      */
18     public static void load(String filePath, String showName) throws Exception{
19         HttpServletResponse response = ServletActionContext.getResponse();    
20         InputStream stream = new FileInputStream(filePath);
21         response.setContentType("APPLICATION/DOWNLOAD");
22         response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(showName, "UTF-8"));
23         response.setContentLength(stream.available());
24         OutputStream os = response.getOutputStream();
25         int iBytesRead = 0;
26         byte[] buffer = new byte[10240000];
27         while ((iBytesRead = stream.read(buffer, 0, 10240000)) != -1) {
28             os.write(buffer, 0, iBytesRead);
29         }
30         os.close();
31         stream.close();
32         response.flushBuffer();
33         File f = new File(filePath);
34         if (f.exists()) {
35             f.delete();
36         }
37     }
38 }
View Code

类似这样的弹出框:

 

转载于:https://www.cnblogs.com/xiaojiayi/p/4780104.html

Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐