首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将HttpServletResponse响应传入Spring Boot的REST控制器?

如何将HttpServletResponse响应传入Spring Boot的REST控制器?
EN

Stack Overflow用户
提问于 2017-05-12 15:44:52
回答 3查看 9K关注 0票数 1

我正在努力让HttpServletResponse response进入Spring Boot的REST控制器。其背后的目的是我想要从REST控制器返回一个文件流。

下面是代码。

代码语言:javascript
复制
@Component
@Api(value = "/api/1/download", description = "act upon selected file.")
@Path("/api/1/download")
@Consumes(MediaType.APPLICATION_JSON)
@RestController
public class DownloadResource {
    private final Logger log = LoggerFactory.getLogger(DownloadResource.class);

    @Autowired
    HttpServletResponse response;

    @ApiOperation(value = "Download a selected file", notes = "allows to download a selected file")
    @Path("/downloadFile")
    @POST
    @Autowired
    public void download(Object fileObject)  {
        String name = (String) ((LinkedHashMap) fileObject).get("path");
        response.setContentType("text/plain");
        response.addHeader("Content-Disposition", "attachment; filename=abcd.txt");
        try
        {
            Files.copy(Paths.get(name), response.getOutputStream());
            response.getOutputStream().flush();
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }

    }

}

既没有下载文件,也没有抛出错误。请帮忙提个建议。谢谢。

EN

回答 3

Stack Overflow用户

发布于 2017-05-12 17:04:56

试一试。我明白你想做什么了吗?

代码语言:javascript
复制
@SpringBootApplication
@RestController
public class FiledownloadApplication {

    public static void main(String[] args) {
        SpringApplication.run(FiledownloadApplication.class, args);
    }


    @PostMapping("/downloadFile")
    public ResponseEntity<FileSystemResource> download(@RequestBody FileDownload fileDownload) throws IOException {
        String path = fileDownload.getPath();
        FileSystemResource fileSystemResource = new FileSystemResource(path);
        return new ResponseEntity<>(fileSystemResource, HttpStatus.OK);
    }

    class FileDownload {

        String path;

        public FileDownload() {
        }

        public String getPath() {
            return path;
        }

        public void setPath(String path) {
            this.path = path;
        }
    }
}
票数 3
EN

Stack Overflow用户

发布于 2017-08-24 18:12:40

我认为应该是这样的。

代码语言:javascript
复制
public ResponseEntity<FileSystemResource> download(@RequestBody FileDownload fileDownload, HttpServletResponse response) throws IOException { ... }

Spring将为您设置响应对象。您试图做的是注入bean "HttpServletResponse“,这没有多大意义,因为它不是一个bean。

票数 2
EN

Stack Overflow用户

发布于 2021-02-15 23:43:18

根据本文档https://docs.spring.io/spring-framework/docs/3.0.0.RC2/reference/html/ch15s03.html#mvc-ann-requestmapping-arguments

您可以简单地以任意顺序将HttpServletResponse声明为下载方法的参数,即

代码语言:javascript
复制
public void download(Object fileObject, HttpServletResponse response)  {

或者,对于您的特定需求,您可以直接参考OutputStream,即

代码语言:javascript
复制
public void download(Object fileObject, OutputStream out)  {

在使用这两种方法中的任何一种时,都应该使用void作为方法的返回类型

或者,另一个值得一看的替代方案是使用StreamingResponseBody

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43932163

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档