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 }
类似这样的弹出框:
时间: 2024-10-22 05:43:02