5
我使用下面的代碼作爲GWT-RPC的GWT服務器端類(servlet)的一部分。如何使用servlet獲取圖像並使用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();
}
}
當我按下客戶端上的按鈕時,servlet正在運行。我想使用Image類將圖像加載到客戶端,但我不知道如何從servlet獲取圖像的URL到客戶端的代碼以顯示它。這是正確的程序還是有另一種方式?我爲客戶端和GWT-RPC使用GWT進行客戶端 - 服務器通信。
如果我有一組圖片,他們可以由你的例子中的單個servlet顯示? –
是的,你需要傳遞一個參數給servlet:'/ images?name = something' –
然後你可以通過'String param = req.getParameter(「name」)' –