xiexiang 1 år sedan
förälder
incheckning
fba1aa5a93
1 ändrade filer med 59 tillägg och 1 borttagningar
  1. 59 1
      src/main/java/com/example/fms/controller/FileMangerController.java

+ 59 - 1
src/main/java/com/example/fms/controller/FileMangerController.java

@@ -77,10 +77,68 @@ public class FileMangerController {
                 try {
                     //文件原始名中的中文字符可能会乱码,对文件名进行URL编码
                     String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString());
+                    String s = systemFile.getOriginalName().substring(systemFile.getOriginalName().indexOf("."));
+                    String contentType = "application/octet-stream"; // 默认二进制流
+                    switch (s) {
+                        case ".xlsx":
+                            contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
+                            break;
+                        case ".xls":
+                        case ".xlt":
+                        case ".xla":
+                            contentType = "application/vnd.ms-excel";
+                            break;
+                        case ".pdf":
+                            contentType = "application/pdf";
+                            break;
+                        case ".doc":
+                        case ".dot":
+                            contentType = "application/msword";
+                            break;
+                        case ".docx":
+                            contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
+                            break;
+                        case ".dotx":
+                            contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.template";
+                            break;
+                        case ".docm":
+                            contentType = "application/vnd.ms-word.document.macroEnabled.12";
+                            break;
+                        case ".dotm":
+                            contentType = "application/vnd.ms-word.template.macroEnabled.12";
+                            break;
+                        case ".ppt":
+                        case ".pot":
+                        case ".pps":
+                        case ".ppa":
+                            contentType = "application/vnd.ms-powerpoint";
+                            break;
+                        case ".gif":
+                            contentType = "image/gif";
+                            break;
+                        case ".jpg":
+                        case ".jpeg":
+                        case ".jpe":
+                        case ".jfif":
+                            contentType = "image/jpeg";
+                            break;
+                        case ".tiff":
+                        case ".tif":
+                            contentType = "image/tiff";
+                            break;
+                        case ".png":
+                            contentType = "image/png";
+                            break;
+                        // 添加其他文件类型的case
+                        // ...
+                        default:
+                            // 如果不是已知的文件类型,则作为二进制流返回
+                            break;
+                    }
                     return ResponseEntity.ok()
                             .contentLength(fileData.length)
                             .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + encodedFileName + "\"")
-                            .contentType(MediaType.parseMediaType("application/octet-stream"))
+                            .contentType(MediaType.parseMediaType(contentType))
                             .body(new InputStreamResource(new ByteArrayInputStream(fileData)));
                 } catch (UnsupportedEncodingException e){
                     throw new XiaoShiException("文件名取出时发生错误!");