Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring boot下载文件只是在浏览器中显示,而不是实际下载_Java_Spring_Spring Boot_File Io - Fatal编程技术网

Java Spring boot下载文件只是在浏览器中显示,而不是实际下载

Java Spring boot下载文件只是在浏览器中显示,而不是实际下载,java,spring,spring-boot,file-io,Java,Spring,Spring Boot,File Io,我举了一个例子: @RequestMapping(value = "/files/{file_name}", method = RequestMethod.GET) @ResponseBody public FileSystemResource getFile(@PathVariable("file_name") String fileName) { return new FileSystemResource(myService.getFileFor(fileName)); } 从该

我举了一个例子:

@RequestMapping(value = "/files/{file_name}", method = RequestMethod.GET)
@ResponseBody
public FileSystemResource getFile(@PathVariable("file_name") String fileName) {
    return new FileSystemResource(myService.getFileFor(fileName)); 
}
从该线程:

这可以正常工作,但浏览器不会下载文件,而是显示它

在有人提到要添加的评论中:

produces = MediaType.APPLICATION_OCTET_STREAM_VALUE

以强制浏览器下载文件。然而,这对我来说仍然不起作用。有人知道如何强制下载吗?

您尚未阅读共享的链接,需要写入响应输出流,还需要删除@ResponseBody@EssexBoy我使用的是该链接中的第二个示例,而不是第一个。我没有漏掉任何东西,对吗?你需要使用公认的答案,这将起作用fine@EssexBoy也试过了,同样的问题。