|
@@ -40,28 +40,51 @@ public class DownloadController {
|
|
|
// }
|
|
|
|
|
|
@GetMapping("/download")
|
|
|
- private void download(@RequestParam String type, HttpServletRequest req, HttpServletResponse response) throws Exception {
|
|
|
+ private void download(@RequestParam String type, HttpServletRequest req, HttpServletResponse resp) throws Exception {
|
|
|
//读取文件
|
|
|
InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("siatic/file/批量上传变更记录模板.xlsx");
|
|
|
String fileName = "批量上传变更记录模板.xlsx";
|
|
|
InputStream inputStream = new BufferedInputStream(resourceAsStream);
|
|
|
- @SuppressWarnings("resource")
|
|
|
+// @SuppressWarnings("resource")
|
|
|
Workbook workbook = new XSSFWorkbook(inputStream);
|
|
|
- String userAgent = req.getHeader("User-Agent");
|
|
|
- if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
|
|
|
- fileName = new String(fileName.getBytes("gbk"), "utf-8");
|
|
|
- } else {
|
|
|
- // 非IE浏览器的处理:
|
|
|
- fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
|
|
|
+// String userAgent = req.getHeader("User-Agent");
|
|
|
+// if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
|
|
|
+// fileName = new String(fileName.getBytes("gbk"), "utf-8");
|
|
|
+// } else {
|
|
|
+// // 非IE浏览器的处理:
|
|
|
+// fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
|
|
|
+// }
|
|
|
+// response.setHeader("Content-disposition", "attachment; filename=" + fileName);
|
|
|
+// response.setContentType("application/octet-stream;charset=utf-8");
|
|
|
+// response.setCharacterEncoding("UTF-8");
|
|
|
+// // 将流中内容写出去
|
|
|
+// OutputStream outputStream = response.getOutputStream();
|
|
|
+// workbook.write(outputStream);
|
|
|
+// outputStream.flush();
|
|
|
+// outputStream.close();
|
|
|
+
|
|
|
+
|
|
|
+ OutputStream out = null;
|
|
|
+ try {
|
|
|
+ String resultFileName = URLEncoder.encode(fileName, "UTF-8");
|
|
|
+ resp.reset();// 清空输出流
|
|
|
+ resp.setCharacterEncoding("UTF-8");
|
|
|
+ resp.setHeader("Content-disposition", "attachment; filename=" + resultFileName);
|
|
|
+ resp.setContentType("application/txt");
|
|
|
+ out = resp.getOutputStream();
|
|
|
+ workbook.write(out);
|
|
|
+ out.flush();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ if (out != null) {
|
|
|
+ out.close();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
}
|
|
|
- response.setHeader("Content-disposition", "attachment; filename=" + fileName);
|
|
|
- response.setContentType("application/octet-stream;charset=utf-8");
|
|
|
- response.setCharacterEncoding("UTF-8");
|
|
|
- // 将流中内容写出去
|
|
|
- OutputStream outputStream = response.getOutputStream();
|
|
|
- workbook.write(outputStream);
|
|
|
- outputStream.flush();
|
|
|
- outputStream.close();
|
|
|
}
|
|
|
|
|
|
private void downloadWorkbook(HttpServletResponse resp, String fileName, Workbook workbook) {
|