0

I am migrating over some legacy servlet code where this pretty much at the end of the code did the download part.

PrintWriter out = null;
out.println("0 " + xyz);
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + test + abc + ".xyz");

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

@GetMapping(value = "/testapi/{id}/{mode}")
public @ResponseBody
ResponseEntity<FileSystemResource> test(HttpServletResponse response, @RequestHeader(value = "Authorization") String authHeader,
       @PathVariable(value = "id") String id, @PathVariable(value = "mode") String mode) throws Exception {

PrintWriter out = null;
out.println("0 " + xyz);
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + test + abc + ".xyz");
 
return response;   

}

I don't know enough about PrintWriter and how they can be used with Spring Controllers so any guidance here would be appreciated.

p0tta
  • 1,461
  • 6
  • 28
  • 49
  • Where are you obtaining the data for the response? It's unusual for a `text/html` to be any sort of download. – chrylis -cautiouslyoptimistic- Sep 07 '20 at 21:56
  • 1
    Does this answer your question? [Downloading a file from spring controllers](https://stackoverflow.com/questions/5673260/downloading-a-file-from-spring-controllers) – randal4 Sep 07 '20 at 21:59
  • No it does not answer my question because mine is specific to using PrintWriter as an output stream. I cannot do without using PrintWriter. I also tried to force a flush on response with no luck. – p0tta Sep 07 '20 at 22:16
  • For the file type, i'm using a specific file type which I cannot disclose here but the original servlet code which works has the type text/html and it works fine. – p0tta Sep 07 '20 at 22:17

0 Answers0