「Spring入門」HttpServletResponseとFileSystemResourceでファイルをダウンロードするコード

方法1–HttpServletResponse
@RequestMapping(“/download")
public void download(HttpServletResponse res) throws IOException {
File file = new File(“sample.xml");
res.setContentLength((int) file.length());
res.setContentType(MediaType.APPLICATION_XML_VALUE);
FileCopyUtils.copy(new FileInputStream(file), res.getOutputStream());
}

方法2–MediaType.APPLICATION_XML_VALUE
@RequestMapping(value = “/fileDownLoad", produces = MediaType.APPLICATION_XML_VALUE)
public Resource fileDownLoad() {
return new FileSystemResource(“sample.xml");
}

Java

Posted by arkgame