GWT-RPC の GWT サーバー側クラス (サーブレット) の一部として、次のコードを使用しています。
private void getImage() {
HttpServletResponse res = this.getThreadLocalResponse();
try {
// Set content type
res.setContentType("image/png");
// Set content size
File file = new File("C:\\Documents and Settings\\User\\image.png");
res.setContentLength((int) file.length());
// Open the file and output streams
FileInputStream in = new FileInputStream(file);
OutputStream out = res.getOutputStream();
// Copy the contents of the file to the output stream
byte[] buf = new byte[1024];
int count = 0;
while ((count = in.read(buf)) >= 0) {
out.write(buf, 0, count);
}
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
クライアントのボタンを押すと、サーブレットが実行されます。Image クラスを使用して画像をクライアントにロードしたいのですが、画像を表示するためにサーブレットからクライアントのコードに画像の URL を取得する方法がわかりません。これは正しい手順ですか、それとも別の方法がありますか? クライアントには GWT を使用し、クライアントとサーバー間の通信には GWT-RPC を使用します。