title: Download a file using Spring controllers and PrintWriter
body: <p>I am migrating over some legacy servlet code where this pretty much at the end of the code did the download part.</p>
  <pre><code>PrintWriter out = null;
  out.println(&quot;0 &quot; + xyz);
  response.setContentType(&quot;text/html;charset=UTF-8&quot;);
  response.setCharacterEncoding(&quot;UTF-8&quot;);
  response.setHeader(&quot;Content-disposition&quot;, &quot;attachment;filename=&quot; + test + abc + &quot;.xyz&quot;);
  </code></pre>
  <p>I am trying to move this code over to Spring and i'm unable to figure out how my Spring controller should be to get the download working. Right now I have the following</p>
  <pre><code>@GetMapping(value = &quot;/testapi/{id}/{mode}&quot;)
  public @ResponseBody
  ResponseEntity&lt;FileSystemResource&gt; test(HttpServletResponse response, @RequestHeader(value = &quot;Authorization&quot;) String authHeader,
         @PathVariable(value = &quot;id&quot;) String id, @PathVariable(value = &quot;mode&quot;) String mode) throws Exception {
  
  PrintWriter out = null;
  out.println(&quot;0 &quot; + xyz);
  response.setContentType(&quot;text/html;charset=UTF-8&quot;);
  response.setCharacterEncoding(&quot;UTF-8&quot;);
  response.setHeader(&quot;Content-disposition&quot;, &quot;attachment;filename=&quot; + test + abc + &quot;.xyz&quot;);
   
  return response;   
  </code></pre>
  <p>}</p>
  <p>I don't know enough about PrintWriter and how they can be used with Spring Controllers so any guidance here would be appreciated.</p>