「Spring」HttpServletResponseでファイルのダウンロードをする

2021年4月7日

書式
@RequestMapping(value ="/関数名",produces="MediaType.APPLICATION_XML_VALUE")
方法1

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@RequestMapping(value ="/fileDownload",produces="MediaType.APPLICATION_XML_VALUE")
public Resource xmlDownload() {
return new FileSystemResource("sample.xml");
}
@RequestMapping(value ="/fileDownload",produces="MediaType.APPLICATION_XML_VALUE") public Resource xmlDownload() { return new FileSystemResource("sample.xml"); }
@RequestMapping(value ="/fileDownload",produces="MediaType.APPLICATION_XML_VALUE")
public Resource xmlDownload() {
  return new FileSystemResource("sample.xml");
}

方法2

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@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());
}
@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()); }
@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());
}

 

SpringMVC

Posted by arkgame