title: Return file from Spring @Controller having OutputStream
body: <p>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. </p>
  
  <p>So the flow is something like that:</p>
  
  <p>getting outputstream <strong>-></strong> service passes this outputstream to controller <strong>-></strong> controller has to send it to a user</p>
  
  <p>I think I need inputstream to do it and I have also found Apache Commons api feature that looks like this: </p>
  
  <pre><code>IOUtils.copy(InputStream is, OutputStream os)
  </code></pre>
  
  <p>but the problem is, it converts it to the other side -> not from <strong>os</strong> to <strong>is</strong>, but from <strong>is</strong> to <strong>os</strong>.</p>
  
  <h1>Edit</h1>
  
  <p>to be clear, because I see the answers are not hitting right thing:<br>
  I use Dropbox api and recieve file in OutputStream and I want this output stream to be sent to user while entering some URL</p>
  
  <pre><code>FileOutputStream outputStream = new FileOutputStream(); //can be any instance of OutputStream
  DbxEntry.File downloadedFile = client.getFile("/fileName.mp3", null, outputStream);
  </code></pre>
  
  <p>Thats why i was talking about converting <strong>outputstream</strong> to <strong>inputstream</strong>, 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)</p>
  
  <p>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</p>
  
  <h1>Edit 2</h1>
  
  <p>The "flow" of my app is something like this:  @Joeblade</p>
  
  <ol>
  <li><p>User enters url: <strong>/download/{file_name}</strong></p></li>
  <li><p>Spring Controller captures the url and calls the <strong>@Service layer to download</strong> the file and pass it to that controller:</p>
  
  <pre><code>@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); // &lt;- and this file(InputStream/OutputStream/byte[] array/File object/MultipartFile I dont really know..) has to be sent to the user
  }
  </code></pre></li>
  <li><p>Now the @Service calls <em>Dropbox API</em> and downloads the file by specified <strong>file_name</strong>, 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:</p>
  
  <pre><code>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;
  }
  </code></pre></li>
  <li><p><strong>Controler recieves the file</strong> (I dont know, at all, in which form as I said upper) and passes it then <strong>to the user</strong></p></li>
  </ol>
  
  <p>So the thing is to recieve <code>OutputStream</code> with the downloaded file by calling <code>dropboxClient.getFile()</code> method and then this <code>OutputStream</code> that contains the downloaded file, has to be sent to the user, how to do this?</p>