gpt4 book ai didi

java - 下载时,少数内容类型的文件扩展名发生变化

转载 作者:行者123 更新时间:2023-12-02 01:59:27 25 4
gpt4 key购买 nike

我现在使用下面的方法从服务器下载文件,对于几个带有扩展名的文件(“.png”,“.txt”,“.pdf”),文件下载正确,但至于(“.exe”) ") 文件正在下载为 f.txt,我可以知道这段代码有什么问题吗?对于 .exe 文件,内容将被写入“f.txt”文件。我还使用 Files.probeContentType(file.toPath()); 来确定媒体类型并将其设置为响应实体的内容类型。提前致谢! :)。扩展名更改为 application/x-msdownload

的媒体类型
        public ResponseEntity<InputStreamResource> getFileFromDisk(String filename) throws IOException {
ResponseEntity<InputStreamResource> filedata = null;
String file_path = "/e:/filesfolder/" + filename;
String fileType = "Undetermined";
final File file = new File(file_path);
fileType = Files.probeContentType(file.toPath());
System.out.println(fileType);
InputStreamResource resource = new InputStreamResource(new FileInputStream(file_path));
String file_checksum = checksumcalc.calculateChecksum(file_path);
filedata = ResponseEntity.ok().header("Md5", file_checksum)
.contentType(MediaType.parseMediaType(fileType)).body(resource);
return filedata;
}

最佳答案

要避免 f.txt 问题,您必须定义 Content-disposition header 来指定附件的文件名。

例如:

filedata = ResponseEntity.ok()
.header("Content-disposition", "attachment; filename=" + fileName)
.header("Md5", file_checksum)
.contentType(MediaType.parseMediaType(fileType)).body(resource);

关于java - 下载时,少数内容类型的文件扩展名发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51854419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com