|
@@ -18,6 +18,8 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
@@ -26,6 +28,7 @@ import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.IOException;
|
|
|
import java.io.OutputStream;
|
|
|
import java.net.URLEncoder;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.nio.file.Files;
|
|
|
import java.nio.file.Path;
|
|
|
import java.nio.file.Paths;
|
|
@@ -85,7 +88,7 @@ public class WorkResourceLibraryController {
|
|
|
RequsetData<String> result = new RequsetData<>();
|
|
|
model.setCreatedatetime(new Date());
|
|
|
model.setFileurl(model.getFileid());
|
|
|
- model.setExtendedname(".pdf");
|
|
|
+ // model.setExtendedname(".pdf");
|
|
|
result = workResourceLibraryService.saveFile(model, userService.getLoginUser().getUserid());
|
|
|
|
|
|
return result;
|
|
@@ -230,12 +233,12 @@ public class WorkResourceLibraryController {
|
|
|
String fileExt=fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
|
|
|
|
|
|
if(!appConfig.safeExts.contains(fileExt)){
|
|
|
- res.setSuccess(false);
|
|
|
- res.setMsg("上传失败:只能是pdf文件!");
|
|
|
- return res;
|
|
|
+ // res.setSuccess(false);
|
|
|
+ // res.setMsg("上传失败:只能是pdf文件!");
|
|
|
+ // return res;
|
|
|
}
|
|
|
|
|
|
- Path pFileName = Paths.get(appConfig.fileMgrUrl, fileId+".pdf");
|
|
|
+ Path pFileName = Paths.get(appConfig.fileMgrUrl, fileId+fileExt);
|
|
|
if(Files.exists(pFileName)){
|
|
|
Files.delete(pFileName);
|
|
|
}
|
|
@@ -253,39 +256,38 @@ public class WorkResourceLibraryController {
|
|
|
|
|
|
@RequestMapping({"/downDoc/{fileId}"})
|
|
|
@ResponseBody
|
|
|
- public String downDoc(HttpServletResponse response, HttpServletRequest request, @PathVariable("fileId") String fileId) {
|
|
|
-
|
|
|
+ public ResponseEntity<String> downDoc(
|
|
|
+ HttpServletResponse response,
|
|
|
+ HttpServletRequest request,
|
|
|
+ @PathVariable("fileId") String fileId) {
|
|
|
DjWorkresourcelibrary file = workResourceLibraryService.getFileInfo(fileId);
|
|
|
- if (file != null) {
|
|
|
- Path pFileName = Paths.get(appConfig.fileMgrUrl, file.getFileurl() + file.getExtendedname());
|
|
|
- try {
|
|
|
- if (Files.exists(pFileName)) {
|
|
|
- response.setContentType("application/force-download");// 设置强制下载不打开
|
|
|
- response.setContentType("multipart/form-data;charset=UTF-8");
|
|
|
-
|
|
|
- String fileName = file.getFilename() + file.getExtendedname();
|
|
|
- String downloadFileName;
|
|
|
- String agent = request.getHeader("USER-AGENT");
|
|
|
- if (agent != null && agent.toLowerCase().indexOf("firefox") > 0) {
|
|
|
- downloadFileName = "=?UTF-8?B?" + (new String(Base64.getEncoder().encodeToString(fileName.getBytes("UTF-8")))) + "?=";
|
|
|
- } else {
|
|
|
- downloadFileName = URLEncoder.encode(fileName, "UTF-8");
|
|
|
- }
|
|
|
-
|
|
|
- response.addHeader("Content-Disposition",
|
|
|
- "attachment;fileName=" + downloadFileName);// 设置文件名
|
|
|
-
|
|
|
-
|
|
|
- OutputStream os = response.getOutputStream();
|
|
|
- Files.copy(pFileName, os);
|
|
|
- }
|
|
|
-
|
|
|
- } catch (Exception ex) {
|
|
|
- fileId = ex.getMessage();
|
|
|
- logger.info("downDoc:" + ex.getMessage());
|
|
|
+ if (file == null) {
|
|
|
+ return ResponseEntity.status(HttpStatus.NOT_FOUND).body("文件不存在");
|
|
|
+ }
|
|
|
+ Path pFileName = Paths.get(appConfig.fileMgrUrl, file.getFileurl() + file.getExtendedname());
|
|
|
+ if (!Files.exists(pFileName)) {
|
|
|
+ return ResponseEntity.status(HttpStatus.NOT_FOUND).body("未查找到此文件");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ // 关键修复1: 根据文件扩展名动态识别 MIME 类型
|
|
|
+ String mimeType = Files.probeContentType(pFileName);
|
|
|
+ if (mimeType == null) {
|
|
|
+ mimeType = "application/octet-stream"; // 默认二进制流类型(强制下载)
|
|
|
}
|
|
|
+ response.setContentType(mimeType);
|
|
|
+ // 关键修复2: 标准化的文件名编码处理
|
|
|
+ String fileName = file.getFilename() + file.getExtendedname();
|
|
|
+ String encodedFileName = URLEncoder.encode(fileName, String.valueOf(StandardCharsets.UTF_8)).replace("+", "%20");
|
|
|
+ String contentDisposition = String.format("attachment; filename=\"%s\"; filename*=UTF-8''%s",
|
|
|
+ encodedFileName, encodedFileName);
|
|
|
+ response.setHeader("Content-Disposition", contentDisposition);
|
|
|
+ // 流式写入响应内容
|
|
|
+ Files.copy(pFileName, response.getOutputStream());
|
|
|
+ return ResponseEntity.ok().build();
|
|
|
+ } catch (IOException ex) {
|
|
|
+ logger.error("Download failed: {}", ex.getMessage());
|
|
|
+ return ResponseEntity.ok().build();
|
|
|
}
|
|
|
- return fileId;
|
|
|
}
|
|
|
|
|
|
|