一般会用在文件下载时,需要在浏览器中打开预览的场景,会有需求获取到对应文件的 mimeType

  1. package cn.mrcode;
  2. import java.io.IOException;
  3. import java.nio.file.Files;
  4. import java.nio.file.Path;
  5. import java.nio.file.Paths;
  6. import java.util.Arrays;
  7. import java.util.List;
  8. /**
  9. * 根据文件名获取 mimeType
  10. *
  11. * @author mrcode
  12. * @date 2021/9/1 20:59
  13. */
  14. public class MimeTypeUtil {
  15. /**
  16. * 流类型
  17. */
  18. public static final String APPLICATION_OCTET_STREAM_VALUE = "application/octet-stream";
  19. /**
  20. * 根据文件名后缀获取 mimeType;
  21. * <pre>
  22. * 支持的类型如下:
  23. * gif : image/gif
  24. * bmp : image/bmp
  25. * ico : image/x-icon
  26. * jpeg : image/jpeg
  27. * jpg : image/jpeg
  28. * png : image/png
  29. * zip : application/x-zip-compressed
  30. * jsp : null
  31. * pdf : application/pdf
  32. * png : image/png
  33. * jpg : image/jpeg
  34. * mp4 : video/mp4
  35. * flv : null
  36. * ppt : application/vnd.ms-powerpoint
  37. * pptx : application/vnd.openxmlformats-officedocument.presentationml.presentation
  38. * xlsx : application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
  39. * xls : application/vnd.ms-excel
  40. * doc : application/msword
  41. * docx : application/vnd.openxmlformats-officedocument.wordprocessingml.document
  42. * txt : text/plain
  43. * </pre>
  44. *
  45. * @return
  46. */
  47. public static String get(String fileName) {
  48. return get(Paths.get(fileName));
  49. }
  50. public static String get(String fileName, String defaultMimeType) {
  51. final String mimeType = get(fileName);
  52. return mimeType == null ? defaultMimeType : mimeType;
  53. }
  54. public static String get(Path filePath) {
  55. try {
  56. return Files.probeContentType(filePath);
  57. } catch (IOException e) {
  58. e.printStackTrace();
  59. return null;
  60. }
  61. }
  62. public static String get(Path filePath, String defaultMimeType) {
  63. final String mimeType = get(filePath);
  64. return mimeType == null ? defaultMimeType : mimeType;
  65. }
  66. public static void main(String[] args) {
  67. final List<String> suffixs = Arrays.asList(
  68. "gif", "bmp", "ico", "jpeg", "jpg", "png", "zip", "jsp", "pdf", "png", "jpg", "mp4", "flv", "ppt", "pptx", "xlsx", "xls", "doc", "docx", "txt"
  69. );
  70. for (int i = 0; i < suffixs.size(); i++) {
  71. final String suffix = suffixs.get(i);
  72. System.out.println(suffix + " : " + get(i + "." + suffix));
  73. }
  74. }
  75. }

文件下载、预览

使用上面的工具类获取 contentType,然后按前端传递的参数 type 来判定是在浏览器内部打开?还是使用浏览器下载

  1. // 设置文件大小
  2. response.setContentLength((int) Files.size(path));
  3. response.setContentType(MimeTypeUtil.get(path, MimeTypeUtil.APPLICATION_OCTET_STREAM_VALUE));
  4. // 文件名有中文,先编码
  5. String fileNameEncod = URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString());
  6. String contentDispositionValue = StrUtil.format("{}; filename=\"{}\"; filename*=utf-8''{}",
  7. type == 1 ? "inline" : "attachment", // inline 浏览器内部打开,attachment 下载
  8. fileNameEncod, fileNameEncod
  9. );
  10. response.setHeader("Content-Disposition", contentDispositionValue);
  11. // java.nio.file.Files
  12. Files.copy(path, response.getOutputStream());

openJdk 无法获取到任何类型

原因:

  • 服务器上使用 OpenJdk:使用上面的写法无法获取到任何类型,应该是对应的配置文件被阉割了
  • 本地使用 OracleJdk:可以正常获取

故而直接改成手动获取的返回了

  1. package com.runshopstore.crmbe.common_service;
  2. import java.nio.file.Path;
  3. import java.nio.file.Paths;
  4. import java.util.Arrays;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import cn.hutool.core.io.file.FileNameUtil;
  9. import cn.hutool.core.util.StrUtil;
  10. import lombok.SneakyThrows;
  11. import lombok.extern.slf4j.Slf4j;
  12. /**
  13. * 根据文件名获取 mimeType
  14. *
  15. * @author mrcode
  16. * @date 2021/9/1 20:59
  17. */
  18. @Slf4j
  19. public class MimeTypeUtil {
  20. private static Map<String, String> map = new HashMap<>();
  21. static {
  22. map.put("gif", "image/gif");
  23. map.put("bmp", "image/bmp");
  24. map.put("ico", "image/x-icon");
  25. map.put("jpeg", "image/jpeg");
  26. map.put("jpg", "image/jpeg");
  27. map.put("png", "image/png");
  28. map.put("zip", "application/x-zip-compressed");
  29. map.put("jsp", "null");
  30. map.put("pdf", "application/pdf");
  31. map.put("png", "image/png");
  32. map.put("jpg", "image/jpeg");
  33. map.put("mp4", "video/mp4");
  34. map.put("flv", "null");
  35. map.put("ppt", "application/vnd.ms-powerpoint");
  36. map.put("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation");
  37. map.put("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  38. map.put("xls", "application/vnd.ms-excel");
  39. map.put("doc", "application/msword");
  40. map.put("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
  41. map.put("txt", "text/plain");
  42. }
  43. /**
  44. * 流类型
  45. */
  46. public static final String APPLICATION_OCTET_STREAM_VALUE = "application/octet-stream";
  47. /**
  48. * 根据文件名后缀获取 mimeType;
  49. * <pre>
  50. * 支持的类型如下:
  51. * gif : image/gif
  52. * bmp : image/bmp
  53. * ico : image/x-icon
  54. * jpeg : image/jpeg
  55. * jpg : image/jpeg
  56. * png : image/png
  57. * zip : application/x-zip-compressed
  58. * jsp : null
  59. * pdf : application/pdf
  60. * png : image/png
  61. * jpg : image/jpeg
  62. * mp4 : video/mp4
  63. * flv : null
  64. * ppt : application/vnd.ms-powerpoint
  65. * pptx : application/vnd.openxmlformats-officedocument.presentationml.presentation
  66. * xlsx : application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
  67. * xls : application/vnd.ms-excel
  68. * doc : application/msword
  69. * docx : application/vnd.openxmlformats-officedocument.wordprocessingml.document
  70. * txt : text/plain
  71. * </pre>
  72. *
  73. * @return
  74. */
  75. public static String get(String fileName) {
  76. return get(Paths.get(fileName));
  77. }
  78. public static String get(String fileName, String defaultMimeType) {
  79. final String mimeType = get(fileName);
  80. return mimeType == null ? defaultMimeType : mimeType;
  81. }
  82. @SneakyThrows
  83. public static String get(Path filePath) {
  84. final String fileName = filePath.getFileName().toString();
  85. final String extName = FileNameUtil.extName(fileName);
  86. return map.get(extName);
  87. //return Files.probeContentType(filePath); // openJDK 无法获取到对应的类型
  88. }
  89. public static String get(Path filePath, String defaultMimeType) {
  90. final String mimeType = get(filePath);
  91. return mimeType == null ? defaultMimeType : mimeType;
  92. }
  93. public static void main(String[] args) {
  94. final List<String> suffixs = Arrays.asList(
  95. "gif", "bmp", "ico", "jpeg", "jpg", "png", "zip", "jsp", "pdf", "png", "jpg", "mp4", "flv", "ppt", "pptx", "xlsx", "xls", "doc", "docx", "txt"
  96. );
  97. for (int i = 0; i < suffixs.size(); i++) {
  98. final String suffix = suffixs.get(i);
  99. // System.out.println(suffix + " : " + get(i + "." + suffix));
  100. System.out.println(StrUtil.format("map.put(\"{}\",\"{}\");", suffix, get(i + "." + suffix)));
  101. }
  102. }
  103. }