|
@@ -1,5 +1,6 @@
|
|
|
package com.persagy.dmp.rwd.edit.controller;
|
|
|
|
|
|
+import com.persagy.dmp.rwd.edit.model.DownloadModel;
|
|
|
import com.persagy.dmp.rwd.edit.service.DownloadService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.poi.ss.usermodel.Workbook;
|
|
@@ -12,7 +13,8 @@ import org.springframework.web.bind.annotation.RequestParam;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
-import java.io.*;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.OutputStream;
|
|
|
import java.net.URLEncoder;
|
|
|
|
|
|
@Slf4j
|
|
@@ -25,36 +27,35 @@ public class DownloadController {
|
|
|
@Autowired
|
|
|
private DownloadService downloadService;
|
|
|
|
|
|
-// @GetMapping("/download")
|
|
|
-// private void download(@RequestParam String type, HttpServletResponse response) {
|
|
|
-// DownloadModel result = downloadService.download(type);
|
|
|
-// if ("workbook".equalsIgnoreCase(result.getType())) {
|
|
|
-// String fileName = result.getName();
|
|
|
-// downloadWorkbook(response, fileName, result.getWorkbook());
|
|
|
-// }
|
|
|
-// }
|
|
|
-
|
|
|
@GetMapping("/download")
|
|
|
- private void download(@RequestParam String type, HttpServletResponse response) throws Exception {
|
|
|
- //读取文件
|
|
|
- InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("siatic/file/批量上传变更记录模板.xlsx");
|
|
|
- InputStream fis = new BufferedInputStream(resourceAsStream);
|
|
|
- byte[] buffer = new byte[fis.available()];
|
|
|
- fis.read(buffer);
|
|
|
- fis.close();
|
|
|
- // 清空response
|
|
|
- response.reset();
|
|
|
- // 设置response的Header
|
|
|
- response.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode("批量导入标准模板.xls", "utf-8"));
|
|
|
- //response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
|
|
|
- response.addHeader("Content-Length", "" + buffer.length);
|
|
|
- OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
|
|
|
- response.setContentType("application/vnd.ms-excel; charset=utf-8");
|
|
|
- toClient.write(buffer);
|
|
|
- toClient.flush();
|
|
|
- toClient.close();
|
|
|
+ private void download(@RequestParam String type, HttpServletResponse response) {
|
|
|
+ DownloadModel result = downloadService.download(type);
|
|
|
+ if ("workbook".equalsIgnoreCase(result.getType())) {
|
|
|
+ String fileName = result.getName();
|
|
|
+ downloadWorkbook(response, fileName, result.getWorkbook());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+// @GetMapping("/download")
|
|
|
+// private void download(@RequestParam String type, HttpServletResponse response) throws Exception {
|
|
|
+// //读取文件
|
|
|
+// InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("siatic/file/批量上传变更记录模板.xlsx");
|
|
|
+// InputStream fis = new BufferedInputStream(resourceAsStream);
|
|
|
+// byte[] buffer = new byte[fis.available()];
|
|
|
+// fis.read(buffer);
|
|
|
+// fis.close();
|
|
|
+// // 清空response
|
|
|
+// response.reset();
|
|
|
+// // 设置response的Header
|
|
|
+// response.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode("批量上传变更记录模板.xlsx", "utf-8"));
|
|
|
+// response.addHeader("Content-Length", "" + buffer.length);
|
|
|
+// OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
|
|
|
+// response.setContentType("application/vnd.ms-excel; charset=utf-8");
|
|
|
+// toClient.write(buffer);
|
|
|
+// toClient.flush();
|
|
|
+// toClient.close();
|
|
|
+// }
|
|
|
+
|
|
|
private void downloadWorkbook(HttpServletResponse resp, String fileName, Workbook workbook) {
|
|
|
OutputStream out = null;
|
|
|
try {
|
|
@@ -62,7 +63,7 @@ public class DownloadController {
|
|
|
String resultFileName = URLEncoder.encode(fileName, "UTF-8");
|
|
|
resp.setCharacterEncoding("UTF-8");
|
|
|
resp.setHeader("Content-disposition", "attachment; filename=" + resultFileName);// 设定输出文件头
|
|
|
- resp.setContentType("application/msexcel");// 定义输出类型
|
|
|
+ resp.setContentType("application/vnd.ms-excel; charset=utf-8");// 定义输出类型
|
|
|
out = resp.getOutputStream();
|
|
|
workbook.write(out);
|
|
|
out.flush();
|