春季:从REST控制器下载文件 [英] Spring : Download file from REST controller

查看:56
本文介绍了春季:从REST控制器下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Spring项目上工作,制作了一个Web应用程序,因此有一个Tomcat服务器,允许我在Web应用程序中吃午餐.
我在这个Web应用程序上有2个功能,第一个功能是允许我在Postgresql数据库上上传文件(它可以正常工作).但是之后,我希望能够下载该文件.

I work on a Spring project to make a web app so there is a Tomcat server which permit me to lunch my web app.
I've got 2 functionalities on this web app, the first permit me to upload a file on a Postgresql Database (it's works correctly). But after that, i want to be able to download this file.

这是我的服务方式.

public byte[] download(Integer id){
    Line line = getById(Line.class, id);
    int blobLenght;
    byte[] fileToReturn = null;
    Blob file = line.getFile();
    blobLenght = (int)file.length();
    fileToReturn = file.getBytes(1,blobLenght);
    return fileToReturn;
}

在我的RestController上,我有一个带有注释@RequestMapping的方法. 并且此方法返回一个ResponseEntity.在后面的方法中,将LineService插入(@Autowired)在我的类的顶部.

On my RestController, i have a method with annotation @RequestMapping. And this method return a ResponseEntity. On the method after, lineService is inject (@Autowired) on the top of my class.

@RequestMapping(value = "/download", method = RequestMethod.GET)
public ResponseEntity<?> download(@RequestParam("id" Integer id)
{
    byte[] fileToDownload = lineService.download(id);
    return new ResponseEntity<byte[]>(fileToDownload,HttpStatus.OK);
}

我已努力简化该方法.

这是我的问题.确实,我实现了一种方法,该方法可以在我一个接一个地提出一些请求时起作用.但是,服务器必须能够支持同时接收很多请求.现在,当我同时测试许多请求时,我遇到了OutOfMemoryError:Java堆空间.
我尝试在下载"方法上实现Stream,但遇到相同的错误.我对流的实现无法很好地完成. 我尝试了许多在互联网上找到的解决方案,这是我实现的最后一个解决方案:

This is my problem. Indeed, I realize a method which works when i make some requests one by one. But, the server must be able to support receiving a lot of requests at the same time. And for now, when I test with many request in same time, i've got an OutOfMemoryError : Java heap space.
I tried to implement Stream on the method 'download' but I get the same error. My implementation of the streams could not be well done. I tried many solution found on internet and this is the last that I implement :

public ResponseEntity<byte[]> download(Integer id){
    Line line = getById(Line.class, id);
    HttpHeaders headers = new HttpHeaders();
    InputStream is = line.getFile().getBinaryStream;
    byte[] fileToReturn = IOUtils.toByteArray(is);
    headers.setCacheControl(CacheControl.noCache().getHeaderValue());
    ResponseEntity<byte[]> responseEntity = new ResponseEntity<byte[]>(fileToDownload,HttpStatus.OK);
    return responseEntity;
}

请问您知道我如何更新我的程序以根据项目的要求进行回答吗?
如果我行事顺心,您能否告诉我我的错误在哪里,或者您有什么建议吗?

Do you know how i can update my program to answer at the requirement of the project, please ?
And if i'm on the good way could you tell my where is my mistake or if you have some suggestions, please ?

感谢您的帮助!

下午.

推荐答案

我使用流修改了下载"方法,并且可以像我想要的那样正常工作.

I modify the 'download' method with stream and it works correctly like I want.

public void download(Integer id, HttpServletResponse response){
    Line line = getById(Line.class, id);
    InputStream is = line.getFile().getBinaryStream;
    IOUtils.copy(is, response.getOutputStream());
    response.flushBuffer();
}

我的控制器就是这样:

public ResponseEntity<?> download(@RequestParam("id") Integer id, HttpServletResponse response)
{
    lineService.download(id,response);
    return new ResponseEntity<>(HttpStatus.OK);
}

这篇关于春季:从REST控制器下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆