I'm using Spring Boot Java to prompt user to save an XML file.
My code is the following:
public class ExportE125Controller {
@Autowired
@Qualifier("jdbcExportE125")
private JdbcTemplate template;
@RequestMapping(path = "/exportE125", method = RequestMethod.POST,
consumes ="application/x-www-form-urlencoded; charset=utf-8")
public ResponseEntity<byte[]> getE125New(@RequestParam Map<String, String> body,HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
String str = "<?xml version="1.0" encoding="UTF-8"?><note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>"
byte[] output =str.getBytes("UTF-8");
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("charset", "utf-8");
responseHeaders.setContentType(MediaType.valueOf("application/xml"));
responseHeaders.setContentLength(output.length);
responseHeaders.set("Content-disposition", "attachment; filename=file.xml");
return new ResponseEntity<byte[]>(output, responseHeaders, HttpStatus.OK);
}
The correct way of having the content of file.xml should be:
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>