一、思路
Java使用POI導出Excel,采用2萬一個Excel,生成后放到服務器某路徑文件夾下,循環查詢並生成直到數據全部導出到Excel后,打包成Zip壓縮包並循環刪除已被打包的Excel,打包完成后彈窗下載。
目前的效率如下:
1591550 15分鍾
999650 8分鍾
909030 2分鍾
833150 1分鍾
139840 1分鍾
粗略時間,未精確計算。
二、實現代碼
controller層
/** * <p> * Title: OneclickExport * </p> * <p> * Description:一鍵導出所有操作日志 * </p> * * @author Saffichan * @param response * @throws Exception * @throws UnsupportedEncodingException * @throws LogNotFoundException */ @RequestMapping("/oneclickExport") public ModelAndView oneclickExport(LogBO logBO, HttpServletResponse response, HttpSession session, HttpServletRequest request) throws Exception { List<Map<String, Object>> dataList = null; LogBO logs = null; if (session.getAttribute("logBO") == null || session.getAttribute("logBO").equals("")) { logs = new LogBO(); } else { logs = (LogBO) session.getAttribute("logBO"); } int counts = logService.count(logs); int page = 1; int pageSize = 20000; int count = (counts+pageSize-1)/pageSize; if (count > 1) { for (int i = 0; i <= count; i++) { logs.setCurrentPage(page); logs.setPageOfSize(pageSize); List<LogBO> logBOs = logService.selectAll(logs); if (logBOs != null) { // 對象轉為List<Map> dataList = getDataListMethods(logBOs); exportAll(dataList, page, request); page = page + 1; } } // 生成文件名 String fileName = "OperationLog"+ new SimpleDateFormat("yyyyMMDDhh24mmssSSS").format(System.currentTimeMillis()); ZipFileAction zip = new ZipFileAction(); zip.downloadZip(fileName, response, request); } else { logs.setCurrentPage(page); logs.setPageOfSize(pageSize); List<LogBO> logBOs = logService.selectAll(logs); if (logBOs != null) { // 對象轉為List<Map> dataList = getDataListMethods(logBOs); export(dataList, response); } else if (logBOs == null) { return null; } } return null; }
//導出全部 public void exportAll(List<Map<String, Object>> dataList, int page, HttpServletRequest request) { // 欄目 String[] columnName = { getI18n("Log.logUsername"), getI18n("Log.logModule"), getI18n("Log.logMethods"), getI18n("Log.logActionurl"), getI18n("Log.logIP"), getI18n("Log.logDate"), getI18n("Log.logCommite") }; // 取值 String[] valueName = { "username", "module", "methods", "actionurl", "ip", "date", "commite" }; // 生成文件名 String fileName = "OperationLog" +page+ new SimpleDateFormat("yyyyMMDDhh24mmssSSS").format(System.currentTimeMillis()); try { OneCilckUtil.oneClickExportExcel(fileName, columnName, valueName, dataList, request); } catch (Exception e) { e.printStackTrace(); } }
//導出單個Excel public void export(List<Map<String, Object>> dataList, HttpServletResponse response) { // 欄目 String[] columnName = { getI18n("Log.logUsername"), getI18n("Log.logModule"), getI18n("Log.logMethods"), getI18n("Log.logActionurl"), getI18n("Log.logIP"), getI18n("Log.logDate"), getI18n("Log.logCommite") }; // 取值 String[] valueName = { "username", "module", "methods", "actionurl", "ip", "date", "commite" }; // 生成文件名 String fileName = "OperationLog"+ new SimpleDateFormat("yyyyMMDDhh24mmssSSS").format(System.currentTimeMillis()); try { ExportUtil.exportToExcel(fileName, columnName, valueName, dataList, response); } catch (Exception e) { e.printStackTrace(); } }
調用接口
ZipFileAction.java
import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.OutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ZipFileAction { //下載最終壓縮包 public int downloadZip(String fileName, HttpServletResponse response, HttpServletRequest request) throws Exception { // 原文件地址 String sourceFilePath = request.getSession().getServletContext().getRealPath("") + "/ExcelFile"; // 生成Zip壓縮包存放路徑 String zipFilePath = request.getSession().getServletContext().getRealPath("") + "/download"; // 生成文件名 //String sourceFilePath = "D:\\ExcelFile\\"; // String zipFilePath = "D:\\download\\"; try { // 調用FileToZip接口生成壓縮包 boolean flag = FileToZip.fileToZip(sourceFilePath, zipFilePath, fileName); if (flag) { System.out.println("文件打包成功!"); } else { System.out.println("文件打包失敗!"); } // Zip壓縮包文件名 String fileNames = fileName + ".zip"; // Zip壓縮包路徑 String path = request.getSession().getServletContext().getRealPath("") + "/download/" + fileNames; //String path = "D:\\download\\"+fileNames; File file = new File(path); if (file.length() < 1 || file == null) { return 1; } else { response.setCharacterEncoding("UTF-8"); response.setHeader("Content-Disposition", "attachment; filename=" + new String(fileNames.getBytes("ISO8859-1"), "UTF-8")); response.setContentLength((int) file.length()); response.setContentType("application/zip");// 定義輸出類型 FileInputStream fis = new FileInputStream(file); BufferedInputStream buff = new BufferedInputStream(fis); byte[] b = new byte[1024];// 相當於我們的緩存 long k = 0;// 該值用於計算當前實際下載了多少字節 OutputStream myout = response.getOutputStream();// 從response對象中得到輸出流,准備下載 // 開始循環下載 while (k < file.length()) { int j = buff.read(b, 0, 1024); k += j; myout.write(b, 0, j); } // 刷新此輸出流並強制將所有緩沖的輸出字節被寫出 myout.flush(); // 關閉流 myout.close(); buff.close(); fis.close(); // 刪除生成的壓縮包文件 file.delete(); // 刪除項目文件夾下所有的文件 File filedel = new File(sourceFilePath); if (filedel.isDirectory()) { // 如果path表示的是一個目錄 File[] fileList = filedel.listFiles(); for (int i = 0; i < fileList.length; i++) { File delfile = fileList[i]; if (!delfile.isDirectory()) { // 如果文件的不是一個目錄,則刪除 // 刪除文件 delfile.delete(); } } } } } catch (Exception e) { e.printStackTrace(); } return 0; } }
OneCilckUtil.java
import java.io.File; import java.io.FileOutputStream; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.xssf.streaming.SXSSFCell; import org.apache.poi.xssf.streaming.SXSSFRow; import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFFont; public class OneCilckUtil { /** * Excel導出公共模板 * * @param fileName * @param columnName * [ID,名稱,類型...] * @param valueName * [id,name,type...] * @param dataList * 參數 * @param response * @throws Exception */ @SuppressWarnings("deprecation") public static void oneClickExportExcel(String fileName, String[] columnName, String[] valueName, List<Map<String, Object>> dataList, HttpServletRequest request) throws Exception { if (dataList.size() == 0) { return; } // 第一步,創建一個webbook,對應一個Excel文件 SXSSFWorkbook wb = new SXSSFWorkbook(1000); // 第二步,在webbook中添加一個sheet,對應Excel文件中的sheet SXSSFSheet sheet = wb.createSheet(fileName); sheet.setDefaultColumnWidth(15); // 統一設置列寬 // 創建第1行 也就是表頭 SXSSFRow row = sheet.createRow(0); // 第四步,創建表頭單元格樣式 以及表頭的字體樣式 XSSFCellStyle style = (XSSFCellStyle) wb.createCellStyle(); style.setWrapText(true);// 設置自動換行 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 創建一個居中格式 style.setFillForegroundColor(HSSFColor.GREY_40_PERCENT.index); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style.setBottomBorderColor(HSSFColor.BLACK.index); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setBorderTop(HSSFCellStyle.BORDER_THIN); XSSFFont headerFont = (XSSFFont) wb.createFont(); // 創建字體樣式 headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字體加粗 headerFont.setFontHeightInPoints((short) 12); // 設置字體大小 style.setFont(headerFont); // 為標題樣式設置字體樣式 XSSFCellStyle cellStyle = (XSSFCellStyle) wb.createCellStyle(); cellStyle.setWrapText(true);// 設置自動換行 cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 創建一個居中格式 // 設置邊框 cellStyle.setBottomBorderColor(HSSFColor.BLACK.index); cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); // 為數據內容設置特點新單元格樣式2 自動換行 上下居中左右也居中 XSSFCellStyle hssfCellStyle = (XSSFCellStyle) wb.createCellStyle(); hssfCellStyle.setWrapText(true);// 設置自動換行 hssfCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 創建一個上下居中格式 hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中 // 設置邊框 hssfCellStyle.setBottomBorderColor(HSSFColor.BLACK.index); hssfCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); hssfCellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); hssfCellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); hssfCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); // 第四.一步,創建表頭的列 for (int i = 0; i < columnName.length; i++) { SXSSFCell cell = row.createCell(i); cell.setCellValue(columnName[i]); cell.setCellStyle(style); } // 第五步,創建單元格,並設置值 for (int i = 0; i < dataList.size(); i++) { row = sheet.createRow((int) i + 1); // 為數據內容設置特點新單元格樣式1 自動換行 上下居中 SXSSFCell datacell = null; Map<String, Object> map = dataList.get(i); for (int j = 0; j < valueName.length; j++) { datacell = row.createCell(j); datacell.setCellValue(String.valueOf(map.get((String) valueName[j])).equals("null") ? "" : String.valueOf(map.get((String) valueName[j]))); datacell.setCellStyle(hssfCellStyle); } map.clear(); } dataList.clear(); dataList = null; // 第六步,將文件存到瀏覽器設置的下載位置 String filename = fileName + ".xlsx"; String path = request.getSession().getServletContext().getRealPath("") + "/ExcelFile/" + filename; //String path = "D:\\ExcelFile\\" + filename; File file = new File(path); file.setReadOnly(); FileOutputStream os = null; if (file.exists()) { System.out.println(path + "目錄下存在名字為:" + filename + "的文件."); } else { try { os = new FileOutputStream(file); wb.write(os);// 將數據寫出去 } catch (Exception e) { e.printStackTrace(); } finally { // 刷新此輸出流並強制將所有緩沖的輸出字節被寫出 os.flush(); // 關閉流 os.close(); /* * dispose of temporary files backing this workbook on disk 處理在磁盤上備份此工作簿的臨時文件 * SXSSF分配臨時文件,您必須始終清除顯式,通過調用dispose方法 */ wb.dispose(); wb.close(); System.gc(); } } } }
FileToZip.java
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; /** * @author Saffi 將文件夾下面的文件 打包成zip壓縮文件 * @author Saffi * @date 2017-10-16 */ public final class FileToZip { /** * 將存放在sourceFilePath目錄下的源文件,打包成fileName名稱的zip文件,並存放到zipFilePath路徑下 * * @param sourceFilePath * :待壓縮的文件路徑 * @param zipFilePath * :壓縮后存放路徑 * @param fileName * :壓縮后文件的名稱 * @return * @throws IOException */ public static boolean fileToZip(String sourceFilePath, String zipFilePath, String fileName) throws IOException { boolean flag = false; File sourceFile = new File(sourceFilePath); FileInputStream fis = null; BufferedInputStream bis = null; FileOutputStream fos = null; ZipOutputStream zos = null; if (sourceFile.exists() == false) { System.out.println("待壓縮的文件目錄:" + sourceFilePath + "不存在."); } else { try { File zipFile = new File(zipFilePath + "/" + fileName + ".zip"); if (zipFile.exists()) { System.out.println(zipFilePath + "目錄下存在名字為:" + fileName + ".zip" + "打包文件."); } else { File[] sourceFiles = sourceFile.listFiles(); if (null == sourceFiles || sourceFiles.length < 1) { System.out.println("待壓縮的文件目錄:" + sourceFilePath + "里面不存在文件,無需壓縮."); } else { fos = new FileOutputStream(zipFile); zos = new ZipOutputStream(new BufferedOutputStream(fos)); byte[] bufs = new byte[1024 * 10]; for (int i = 0; i < sourceFiles.length; i++) { // 創建ZIP實體,並添加進壓縮包 ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName()); zos.putNextEntry(zipEntry); // 讀取待壓縮的文件並寫進壓縮包里 fis = new FileInputStream(sourceFiles[i]); bis = new BufferedInputStream(fis, 1024 * 10); int read = 0; while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) { zos.write(bufs, 0, read); } if (null != bis) { bis.close(); } } flag = true; if (null != zos) { zos.closeEntry(); zos.close(); } } } } catch (FileNotFoundException e) { e.printStackTrace(); throw new RuntimeException(e); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } finally { // 關閉流 try { if (null != fis) { fis.close(); } if (null != fos) { fos.close(); } } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } } } return flag; } }
ExportUtil.java
/** * 導出工具類 * @author Saffi * */ public class ExportUtil { /** * Excel導出公共模板 * @param fileName * @param columnName [ID,名稱,類型...] * @param valueName [id,name,type...] * @param dataList 參數 * @param response * @throws Exception */ @SuppressWarnings("deprecation") public static void exportToExcel(String fileName, String[] columnName, String[] valueName, List<Map<String, Object>> dataList, HttpServletResponse response) throws Exception { if (dataList.size() == 0) { return; } // 第一步,創建一個webbook,對應一個Excel文件 SXSSFWorkbook wb = new SXSSFWorkbook(1000); // 第二步,在webbook中添加一個sheet,對應Excel文件中的sheet SXSSFSheet sheet = wb.createSheet(fileName); sheet.setDefaultColumnWidth(15); //統一設置列寬 // 創建第1行 也就是表頭 SXSSFRow row = sheet.createRow(0); // 第四步,創建表頭單元格樣式 以及表頭的字體樣式 XSSFCellStyle style = (XSSFCellStyle) wb.createCellStyle(); style.setWrapText(true);// 設置自動換行 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 創建一個居中格式 style.setFillForegroundColor(HSSFColor.GREY_40_PERCENT.index); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style.setBottomBorderColor(HSSFColor.BLACK.index); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setBorderTop(HSSFCellStyle.BORDER_THIN); XSSFFont headerFont = (XSSFFont) wb.createFont(); // 創建字體樣式 headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字體加粗 headerFont.setFontHeightInPoints((short) 12); // 設置字體大小 style.setFont(headerFont); // 為標題樣式設置字體樣式 XSSFCellStyle cellStyle = (XSSFCellStyle) wb.createCellStyle(); cellStyle.setWrapText(true);// 設置自動換行 cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 創建一個居中格式 // 設置邊框 cellStyle.setBottomBorderColor(HSSFColor.BLACK.index); cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); // 為數據內容設置特點新單元格樣式2 自動換行 上下居中左右也居中 XSSFCellStyle hssfCellStyle = (XSSFCellStyle) wb.createCellStyle(); hssfCellStyle.setWrapText(true);// 設置自動換行 hssfCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 創建一個上下居中格式 hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中 // 設置邊框 hssfCellStyle.setBottomBorderColor(HSSFColor.BLACK.index); hssfCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); hssfCellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); hssfCellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); hssfCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); // 第四.一步,創建表頭的列 for (int i = 0; i < columnName.length; i++) { SXSSFCell cell = row.createCell(i); cell.setCellValue(columnName[i]); cell.setCellStyle(style); } // 第五步,創建單元格,並設置值 for (int i = 0; i < dataList.size(); i++) { row = sheet.createRow((int) i + 1); // 為數據內容設置特點新單元格樣式1 自動換行 上下居中 SXSSFCell datacell = null; Map<String, Object> map = dataList.get(i); for (int j = 0; j < valueName.length; j++) { datacell = row.createCell(j); datacell.setCellValue(String.valueOf(map.get((String) valueName[j])).equals("null") ? "" : String.valueOf(map.get((String) valueName[j]))); datacell.setCellStyle(hssfCellStyle); } map.clear(); } dataList.clear(); dataList=null; // 第六步,將文件存到瀏覽器設置的下載位置 String filename = fileName + ".xlsx"; response.setContentType("application/ms-excel;charset=UTF-8"); response.setHeader("Content-Disposition", "attachment;filename=".concat(String.valueOf(URLEncoder.encode(filename, "UTF-8")))); OutputStream out = response.getOutputStream(); try { wb.write(out);// 將數據寫出去 } catch (Exception e) { e.printStackTrace(); } finally { // 刷新此輸出流並強制將所有緩沖的輸出字節被寫出 out.flush(); // 關閉流 out.close(); /* * dispose of temporary files backing this workbook on disk 處理在磁盤上備份此工作簿的臨時文件 * SXSSF分配臨時文件,您必須始終清除顯式,通過調用dispose方法 */ wb.dispose(); wb.close(); System.gc(); } } @SuppressWarnings("deprecation") public static void exportToExcel(String fileName, String[] columnName, List<Object[]> dataList, HttpServletResponse response) throws Exception { if (dataList == null || dataList.size() == 0) { return; } // 第一步,創建一個webbook,對應一個Excel文件 SXSSFWorkbook wb = new SXSSFWorkbook(1000); // 第二步,在webbook中添加一個sheet,對應Excel文件中的sheet SXSSFSheet sheet = wb.createSheet(fileName); sheet.setDefaultColumnWidth(15); //統一設置列寬 // 創建第1行 也就是表頭 SXSSFRow row = sheet.createRow(0); // 第四步,創建表頭單元格樣式 以及表頭的字體樣式 XSSFCellStyle style = (XSSFCellStyle) wb.createCellStyle(); style.setWrapText(true);// 設置自動換行 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 創建一個居中格式 style.setFillForegroundColor(HSSFColor.GREY_40_PERCENT.index); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style.setBottomBorderColor(HSSFColor.BLACK.index); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setBorderTop(HSSFCellStyle.BORDER_THIN); XSSFFont headerFont = (XSSFFont) wb.createFont(); // 創建字體樣式 headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字體加粗 headerFont.setFontHeightInPoints((short) 12); // 設置字體大小 style.setFont(headerFont); // 為標題樣式設置字體樣式 // 第四.一步,創建表頭的列 for (int i = 0; i < columnName.length; i++) { SXSSFCell cell = row.createCell(i); cell.setCellValue(columnName[i]); cell.setCellStyle(style); } // 第五步,創建單元格,並設置值 for (int i = 0; i < dataList.size(); i++) { row = sheet.createRow((int) i + 1); // 為數據內容設置特點新單元格樣式1 自動換行 上下居中 XSSFCellStyle cellStyle = (XSSFCellStyle) wb.createCellStyle(); cellStyle.setWrapText(true);// 設置自動換行 cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 創建一個居中格式 // 設置邊框 cellStyle.setBottomBorderColor(HSSFColor.BLACK.index); cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); // 為數據內容設置特點新單元格樣式2 自動換行 上下居中左右也居中 XSSFCellStyle hssfCellStyle = (XSSFCellStyle) wb.createCellStyle(); hssfCellStyle.setWrapText(true);// 設置自動換行 hssfCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 創建一個上下居中格式 hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中 // 設置邊框 hssfCellStyle.setBottomBorderColor(HSSFColor.BLACK.index); hssfCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); hssfCellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); hssfCellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); hssfCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); SXSSFCell datacell = null; Object[] map = dataList.get(i); for (int j = 0; j < columnName.length; j++) { datacell = row.createCell(j); datacell.setCellValue(String.valueOf(map[j])); datacell.setCellStyle(hssfCellStyle); } map=null; } dataList.clear(); dataList=null; // 第六步,將文件存到瀏覽器設置的下載位置 String filename = fileName + ".xlsx"; response.setContentType("application/ms-excel;charset=UTF-8"); response.setHeader("Content-Disposition", "attachment;filename=".concat(String.valueOf(URLEncoder.encode(filename, "UTF-8")))); OutputStream out = response.getOutputStream(); try { wb.write(out);// 將數據寫出去 } catch (Exception e) { e.printStackTrace(); } finally { // 刷新此輸出流並強制將所有緩沖的輸出字節被寫出 out.flush(); // 關閉流 out.close(); /* * dispose of temporary files backing this workbook on disk 處理在磁盤上備份此工作簿的臨時文件 * SXSSF分配臨時文件,您必須始終清除顯式,通過調用dispose方法 */ wb.dispose(); wb.close(); System.gc(); } } }
因項目使用了Nginx進行反向代理,因此Nginx配置文件也需要設置,以免Nginx網關超時。
#keepalive_timeout 0; keepalive_timeout 65; fastcgi_connect_timeout 200000s; fastcgi_send_timeout 200000s; fastcgi_read_timeout 200000s; #gzip on; server { listen 80; server_name 域名 服務器IP; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://localhost:端口; proxy_set_header Host $host; proxy_set_header Remote_Addr $remote_addr; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 200000s; proxy_send_timeout 200000s; proxy_read_timeout 200000s; }