JAX-RSでは、画像ファイルをダウンロードするには、メソッドに `@Produces(" image/image-type ")`というアノテーションを付けます:
-
"png"画像のサービスメソッドに @ Produces( "image/png") を入れてください.
-
レスポンスヘッダに " Content-Disposition "を設定してダウンロードを促す
ボックス。
1. JAX-RSで画像をダウンロードする
JAX-RSから画像ファイルをダウンロードする完全な例。
import java.io.File;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
@Path("/image")
public class ImageService {
private static final String FILE__PATH = "c:\\mkyong-logo.png";
@GET
@Path("/get")
@Produces("image/png")
public Response getFile() {
File file = new File(FILE__PATH);
ResponseBuilder response = Response.ok((Object) file);
response.header("Content-Disposition",
"attachment; filename=image__from__server.png");
return response.build();
}
}
デモ
このURIパターンにアクセスしてください: " /image/get "。
Figure:新しいファイル名 " image from server.png " を使用して、サーバーからのイメージファイル " c:\\ mkyong-logo.png
ソースコードをダウンロードする
ダウンロードする - JAX-RS-Download-ImageFile-Example.zip (6 KB)
参考文献
@Produces JavaDoc]。 http://en.wikipedia.org/wiki/Internet media type#Type__image—list of
画像タイプ]
リンク://タグ/ダウンロード/[ダウンロード]リンク://タグ/画像/[画像] jax-rs