0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SpringBootでのローカルファイルダウンロード覚書

Posted at

ファイルダウンロード実装手順

はじめに

業務のためのメモ代わりの殴り書き。あとできちんとした記事にする。

ファイルダウンロード

最終的にはControllerから Resouce型のファイルをreturnすればいい
ローカルのファイルをDLさせるのであれば

AnyController.java
Resource resource = new FileSystemResource("ここにファイルパス");
return resource;

ファイルを任意の名前でダウンロードさせたい

Controllerの引数に HttpServeletResponse response を追加。
responseのフィールド変数(header)に適切な値をセットする。
return 等はそのままでいい。Spring側がよしなに対応してくれる。

AnyController.java
// 引数の記述は省略
Resource resource = new FileSystemResource("ここにファイルパス");
response.setHeader("Content-Disposition", "attachment; filename=" + "ここにファイル名");
return resource;

全角文字をファイル名に使いたい

ファイル名をURLエンコードされたものにすればOK

AnyController.java
// 引数の記述は省略
Resource resource = new FileSystemResource("ここにファイルパス");
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("ここにファイル名", "UTF-8"));
return resource;
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?