「Spring」HttpServletResponseでファイルのダウンロードをする
書式
@RequestMapping(value ="/関数名",produces="MediaType.APPLICATION_XML_VALUE")
方法1
@RequestMapping(value ="/fileDownload",produces="MediaType.APPLICATION_XML_VALUE")
public Resource xmlDownload() {
return new FileSystemResource("sample.xml");
}
方法2
@RequestMapping("/xmlDownload")
public void xmlDownload(HttpServletResponse response) throws IOException {
File cft =new File("smaple.xml");
response.setContentLength((int)cft.length());
response.setContentType(MediaType.APPLICATION_XML_VALUE);
FileCopyUtils.copy(new FileInputStream(cft),response.getOutputStream());
}