Spring : Download file from REST controller

≯℡__Kan透↙ 提交于 2020-07-22 07:58:59

问题


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.

This is my method on my service.

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

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

I have made an effort to simplify as much as possible the method.

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 ?

Thanks you for your help !

PM.


回答1:


I hope i helped you. With way work for me.

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

    }
}



回答2:


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

And my controller was like that :

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


来源:https://stackoverflow.com/questions/51519160/spring-download-file-from-rest-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!