使用此代码,我可以从 servlet 呈现图像,但我的业务说。我需要添加一个链接说“www.google.com”。如果我单击此图像。有什么方法可以访问图像带有链接。我需要直接从 servlet 中刷新它,不应该使用 jsp。任何人都可以帮助我。
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
ServletContext sc = getServletContext();
String filename = sc.getRealPath("image.JPG");
resp.setContentType("image/jpeg");
// Set content size
File file = new File(filename);
resp.setContentLength((int)file.length());
// Open the file and output streams
FileInputStream in = new FileInputStream(file);
OutputStream out = resp.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();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request , response);
// TODO Auto-generated method stub
}
}