私はGWT-RPCのためのGWTサーバー側のクラス(サーブレット)の一環として、次のコードを使用しています。GWT Imageクラスを使用してサーブレットで画像を取得して表示する方法は?
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-RPCをクライアントとサーバの通信に使用します。
私がイメージのセットを持っている場合、彼らができますあなたの例のように単一のサーブレットで表示されますか? –
はい、サーブレットにパラメータを渡す必要があります: '/ images?name = something' –
次に、' String param = req.getParameter( "name") 'でサーブレットのパラメータを取得できます。 –