春季:从REST控制器下载文件

时间:2018-07-25 12:41:31

标签: java spring postgresql stream blob

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

这是我的服务方式。

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)在我的类的顶部。

@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,但遇到相同的错误。我对流的实现无法很好地完成。 我尝试了许多在Internet上找到的解决方案,这是我实现的最后一个解决方案:

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;
}

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

感谢您的帮助!

下午。

2 个答案:

答案 0 :(得分:0)

希望我能对您有所帮助。为我工作。

import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.springframework.core.io.Resource;

@RestController
public class SomeClass{


    @GetMapping("/{fileName}")
    public ResponseEntity<Resource> getFile(@PathVariable String fileName, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Resource file = getFileAsResource(fileName);
        HttpHeaders headers = prepareHeaderForFileReturn(fileName, request, response);
        return new ResponseEntity<Resource>(file, headers, HttpStatus.OK);
    }

    private HttpHeaders prepareHeaderForFileReturn(String fileName, HttpServletRequest request,
                                                   HttpServletResponse response) {
        HttpHeaders headers = new HttpHeaders();
        headers.set(HttpHeaders.CONTENT_TYPE, getContentTypeForAttachment(fileName));
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
        return headers;
    }


    public Resource getFileAsResource(String filename) throws FileNotFoundException {
        String filePath = path +  "/" + filename;
        Resource file = loadAsResource(filePath);
        return file;
    }

    private Resource loadAsResource(String filename) throws FileNotFoundException {
        try {
            Path file = Paths.get(filename);
            org.springframework.core.io.Resource resource = new UrlResource(file.toUri());
            if (resource.exists() || resource.isReadable()) {
                return resource;
            } else {
                log.error("Could not read file: " + filename);
                throw new FileNotFoundException();
            }
        } catch (MalformedURLException e) {
            log.error("Could not read file: " + filename, e);
            throw new FileNotFoundException();
        }
    }

    //Obtains a content type for file by his extension.

    public String getContentTypeForAttachment(String fileName) {
        String fileExtension = com.google.common.io.Files.getFileExtension(fileName);
        if (fileExtension.equals("pdf")) return "application/pdf";
        else if (fileExtension.equals("doc")) return "application/msword";
        else if (fileExtension.equals("jpeg")) return "image/jpeg";

    }
}

答案 1 :(得分:0)

我使用流修改了“下载”方法,并且可以按照我的要求正确地工作。

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);
}
相关问题