|
@@ -1,8 +1,8 @@
|
|
package com.persagy.dmp.rwd.edit.controller;
|
|
package com.persagy.dmp.rwd.edit.controller;
|
|
|
|
|
|
-import com.persagy.dmp.rwd.edit.model.DownloadModel;
|
|
|
|
import com.persagy.dmp.rwd.edit.service.DownloadService;
|
|
import com.persagy.dmp.rwd.edit.service.DownloadService;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
import org.apache.poi.ss.usermodel.Workbook;
|
|
import org.apache.poi.ss.usermodel.Workbook;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
@@ -13,7 +13,9 @@ import org.springframework.web.bind.annotation.RequestParam;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
+import java.io.BufferedInputStream;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
|
+import java.io.InputStream;
|
|
import java.io.OutputStream;
|
|
import java.io.OutputStream;
|
|
import java.net.URLEncoder;
|
|
import java.net.URLEncoder;
|
|
|
|
|
|
@@ -27,35 +29,35 @@ public class DownloadController {
|
|
@Autowired
|
|
@Autowired
|
|
private DownloadService downloadService;
|
|
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")
|
|
// @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 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");
|
|
|
|
+ String fileName = "批量上传变更记录模板.xlsx";
|
|
|
|
+ InputStream inputStream = new BufferedInputStream(resourceAsStream);
|
|
|
|
+ @SuppressWarnings("resource")
|
|
|
|
+ HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
|
|
|
|
+ // 非IE浏览器的处理:
|
|
|
|
+ fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
|
|
|
|
+ response.setHeader("Content-disposition", "attachment; filename=" + fileName);
|
|
|
|
+ response.setContentType("application/vnd.ms-excel; 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) {
|
|
private void downloadWorkbook(HttpServletResponse resp, String fileName, Workbook workbook) {
|
|
OutputStream out = null;
|
|
OutputStream out = null;
|
|
try {
|
|
try {
|