LoginSignup
0
0

More than 5 years have passed since last update.

Dropwizard 0.7.1でファイルアップロード時のリクエストから日本語ファイル名を取り出す

Last updated at Posted at 2015-01-30

文字コードはUTF-8として話をすすめる。

DropwizardはRESTアクセスの解析にjerseyを使っているが、ファイルをアップロードする際にファイル名が日本語だと期待通りファイル名を取り出せない。
この問題はこちらのスレッドを見るとjersey 2.2で修正されているようだ。

ところで Dropwizard 0.7.1 が標準で取り込む jersey は1.8系統。
Dropwizard は出来るだけ標準構成で使いたいのでパッケージを差し替えるような無茶な対応はせずシンプルにコードで頑張る。

レスポンスの内容とメソッドの名前が一致していないがそこはご愛嬌。

@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response upload(@FormDataParam("data") InputStream inputStream, @FormDataParam("data") FormDataContentDisposition disposition) {
    String filename = new String(disposition.getFileName().getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
    // ここで何か処理

    return Response.ok(filename.getBytes(StandardCharsets.UTF_8)).build();
}
0
0
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
0