title: How to programmatically consume a file from a Rest API using Spring?
body: <p>I have the following Rest resource which downloads a file from DB. It works fine from the browser, however, when I try to do it from a Java client as below, I get 406 (Not accepted error).</p>
  
  <pre><code>...
   @RequestMapping(value="/download/{name}", method=RequestMethod.GET, 
          produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
  public @ResponseBody HttpEntity&lt;byte[]&gt; downloadActivityJar(@PathVariable String name) throws IOException
  {
      logger.info("downloading : " + name + " ... ");
      byte[] file = IOUtils.toByteArray(artifactRepository.downloadJar(name));
      HttpHeaders header = new HttpHeaders();
      header.set("Content-Disposition", "attachment; filename="+ name + ".jar");
      header.setContentLength(file.length);
  
      return new HttpEntity&lt;byte[]&gt;(file, header);
  }
  ...
  </code></pre>
  
  <p>The client is deployed on the same server with different port (message gives the correct name) :</p>
  
  <pre><code>    ...
      RestTemplate restTemplate = new RestTemplate();
      String url = "http://localhost:8080/activities/download/" + message.getActivity().getName();
      File jar = restTemplate.getForObject(url, File.class);
      logger.info("File size: " + jar.length() + " Name: " + jar.getName());
      ...
  </code></pre>
  
  <p>What am I missing here?</p>