title: How download file using java spark?
body: <p>I want to write simple rest api for file download.</p>
  
  <p>I cant find docs about it as I understood I need to set <code>mimetype='application/zip'</code> for response, but not clear how to return stream.</p>
  
  <p><a href="http://sparkjava.com/" rel="noreferrer">http://sparkjava.com/</a></p>
  
  <p>update:
  resolved here example code:</p>
  
  <pre><code>public static void main(String[] args) {
      //setPort(8080);
      get("/hello", (request, responce) -&gt; getFile(request,responce));
  }
  
  private static Object getFile(Request request, Response responce) {
      File file = new File("MYFILE");
      responce.raw().setContentType("application/octet-stream");
      responce.raw().setHeader("Content-Disposition","attachment; filename="+file.getName()+".zip");
      try {
  
          try(ZipOutputStream zipOutputStream = new ZipOutputStream(new BufferedOutputStream(responce.raw().getOutputStream()));
          BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file)))
          {
              ZipEntry zipEntry = new ZipEntry(file.getName());
  
              zipOutputStream.putNextEntry(zipEntry);
              byte[] buffer = new byte[1024];
              int len;
              while ((len = bufferedInputStream.read(buffer)) &gt; 0) {
                  zipOutputStream.write(buffer,0,len);
              }
          }
      } catch (Exception e) {
          halt(405,"server error");
      }
  
      return null;
  </code></pre>