「Spring Boot」xmlファイルをダウンロードするサンプル

構文
1.public void setContentLength( int コンテンツサイズ )

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
setContentLengthメソッドで、指定された、コンテンツサイズは、HTTPヘッダーの「Content-Length」にセットされます。
setContentLengthメソッドで、指定された、コンテンツサイズは、HTTPヘッダーの「Content-Length」にセットされます。
setContentLengthメソッドで、指定された、コンテンツサイズは、HTTPヘッダーの「Content-Length」にセットされます。

2.public void setContentType( String コンテンツタイプ名 )
setContentTypeメソッドは、MIMEタイプと、出力するコンテンツタイプ名をセットします。

3.public static int copy(InputStream in,OutputStream out)throws IOExceptionSE
指定されたInputStream の内容を指定された OutputStream にコピーします。完了したら両方のストリームを閉じます。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
引数:
in - コピー元のストリーム
out - コピー先のストリーム
引数: in - コピー元のストリーム out - コピー先のストリーム
引数:
in - コピー元のストリーム
out - コピー先のストリーム

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@RequestMapping("/download")
@ResponseBody
public void download(HttpServletResponse response) throws IOException {
File file = new File("student.xml");
// レスポンスデータのコンテンツサイズを設定
response.setContentLength((int) file.length());
//レスポンスデータのコンテンツタイプをセット
response.setContentType(MediaType.APPLICATION_XML_VALUE);
FileCopyUtils.copy(new FileInputStream(file), res.getOutputStream());
}
@RequestMapping("/download") @ResponseBody public void download(HttpServletResponse response) throws IOException { File file = new File("student.xml"); // レスポンスデータのコンテンツサイズを設定 response.setContentLength((int) file.length()); //レスポンスデータのコンテンツタイプをセット response.setContentType(MediaType.APPLICATION_XML_VALUE); FileCopyUtils.copy(new FileInputStream(file), res.getOutputStream()); }
@RequestMapping("/download")
@ResponseBody
public void download(HttpServletResponse response) throws IOException {
    File file = new File("student.xml");
      
      // レスポンスデータのコンテンツサイズを設定
    response.setContentLength((int) file.length());
      
      //レスポンスデータのコンテンツタイプをセット
    response.setContentType(MediaType.APPLICATION_XML_VALUE);
    FileCopyUtils.copy(new FileInputStream(file), res.getOutputStream());
      
}

 

Spring Boot

Posted by arkgame