Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return file from Spring @Controller having OutputStream

Tags:

I want to return a file from a Spring controller. I already have API that can give me any implementation of OutputStream and then I need to send it to a user.

So the flow is something like that:

getting outputstream -> service passes this outputstream to controller -> controller has to send it to a user

I think I need inputstream to do it and I have also found Apache Commons api feature that looks like this:

IOUtils.copy(InputStream is, OutputStream os) 

but the problem is, it converts it to the other side -> not from os to is, but from is to os.

Edit

to be clear, because I see the answers are not hitting right thing:
I use Dropbox api and recieve file in OutputStream and I want this output stream to be sent to user while entering some URL

FileOutputStream outputStream = new FileOutputStream(); //can be any instance of OutputStream DbxEntry.File downloadedFile = client.getFile("/fileName.mp3", null, outputStream); 

Thats why i was talking about converting outputstream to inputstream, but have no idea how to do it. Furthermore, I suppose that there is better way to solve this (maybe return byte array somehow from outputstream)

I was trying to pass servlet outputstream [response.getOutputstream()] through parameter to the method that downloads file from dropbox, but it didnt work, at all

Edit 2

The "flow" of my app is something like this: @Joeblade

  1. User enters url: /download/{file_name}

  2. Spring Controller captures the url and calls the @Service layer to download the file and pass it to that controller:

    @RequestMapping(value = "download/{name}", method = RequestMethod.GET) public void getFileByName(@PathVariable("name") final String name, HttpServletResponse response) throws IOException {     response.setContentType("audio/mpeg3");     response.setHeader("Content-Disposition", "attachment; filename=" + name);     service.callSomeMethodAndRecieveDownloadedFileInSomeForm(name); // <- and this file(InputStream/OutputStream/byte[] array/File object/MultipartFile I dont really know..) has to be sent to the user } 
  3. Now the @Service calls Dropbox API and downloads the file by specified file_name, and puts it all to the OutputStream, and then passes it (in some form.. maybe OutputStream, byte[] array or any other object - I dont know which is better to use) to the controller:

    public SomeObjectThatContainsFileForExamplePipedInputStream callSomeMethodAndRecieveDownloadedFileInSomeForm(final String name) throws IOException {     //here any instance of OutputStream - it needs to be passed to client.getFile lower (for now it is PipedOutputStream)     PipedInputStream inputStream = new PipedInputStream(); // for now     PipedOutputStream outputStream = new PipedOutputStream(inputStream);       //some dropbox client object     DbxClient client = new DbxClient();     try {         //important part - Dropbox API downloads the file from Dropbox servers to the outputstream object passed as the third parameter         client.getFile("/" + name, null, outputStream);     } catch (DbxException e){         e.printStackTrace();     } finally {         outputStream.close();     }     return inputStream; } 
  4. Controler recieves the file (I dont know, at all, in which form as I said upper) and passes it then to the user

So the thing is to recieve OutputStream with the downloaded file by calling dropboxClient.getFile() method and then this OutputStream that contains the downloaded file, has to be sent to the user, how to do this?

like image 861
azalut Avatar asked Jan 02 '15 11:01

azalut


People also ask

How do I get files from spring boot controller?

Spring Boot file uploader Create a Spring Boot application and include the Spring Web facilities; Create a Spring @Controller class; Add a method to the controller class which takes Spring's MultipartFile as an argument; Save the uploaded file to a directory on the server; and.

Can we return view in rest controller?

@RestController is not meant to be used to return views to be resolved. It is supposed to return data which will be written to the body of the response, hence the inclusion of @ResponseBody .

What is OutputStream Java?

The OutputStream class of the java.io package is an abstract superclass that represents an output stream of bytes. Since OutputStream is an abstract class, it is not useful by itself. However, its subclasses can be used to write data.


1 Answers

Get the OutputStream from the HttpServletResponse and write the file to it (in this example using IOUtils from Apache Commons)

@RequestMapping(value = "/download", method = RequestMethod.GET) public void download(HttpServletResponse response) {     ...     InputStream inputStream = new FileInputStream(new File(PATH_TO_FILE)); //load the file     IOUtils.copy(inputStream, response.getOutputStream());     response.flushBuffer();     ... } 

Make sure you use a try/catch to close the streams in case of an exception.

like image 90
John Smith Avatar answered Sep 28 '22 12:09

John Smith