问题
在进行文件下载的时候,一般都需要配置对应文件的Content-Type响应头。
Java
Path rootLocation = Paths.get("rootPath");
Path path = rootLocation.resolve("filename.png");
// 获取文件的Content-Type
String mimeType = Files.probeContentType(path);
org.springframework.core.io.Resource resource = new UrlResource(path.toUri());
return ResponseEntity.ok()
// 设置Content-Type响应头
.header(HttpHeaders.CONTENT_TYPE, mimeType)
.header(HttpHeaders.CONTENT_DISPOSITION, mimeType.startsWith("image/") ? "inline;filename*=utf-8''" + newFileName : "attachment;filename*=utf-8''" + newFileName)
.body(resource);