我正在处理用户信息被添加和修改(更新)的应用程序。使用servlet在jsp中显示图像
在添加模块,管理员输入用户详细信息并在“添加”按钮生成unique-id(abc001)。并且管理员还将用户的图像/图片(名称:abc001)保存在服务器位置(// some-location-ip address/D drive/images)中。
“更新”模块,admin可以修改用户的详细信息,但不能修改id。
我需要一些方向在几个场景。
如果某个管理员“更新”了某个特定用户,则当该管理员点击更新按钮时,该服务器中该用户的图像应该会显示在该页面上。在JSP
图片代码:
<img height="100px;" width="100px;" src="........." alt="Candidate Image"></img>
我已经写了一个servlet,但不知道如何调用对应于不同用户的不同图像和个人资料页上显示的图像。
用户A简档将显示用户A的图像 用户B简档将显示用户B图像 等
的Servlet代码段
public class UpDatePhoto extends HttpServlet {
public UpDatePhoto() {
super();
// TODO Auto-generated constructor stub
}
private static final long serialVersionUID = -8071854868821235857L;
private static final int DEFAULT_BUFFER_SIZE = 10240; // 10KB.
private String imagePath;
*public void init() throws ServletException {
this.imagePath = "D:\\photo_not_available_large.png";
}*
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String requestedImage = request.getPathInfo();
if (requestedImage == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
return;
}
File image = new File(imagePath, URLDecoder.decode(requestedImage, "UTF-8"));
String contentType = getServletContext().getMimeType(image.getName());
if (contentType == null || !contentType.startsWith("image")) {
response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
return;
}
response.reset();
response.setBufferSize(DEFAULT_BUFFER_SIZE);
response.setContentType(contentType);
response.setHeader("Content-Length", String.valueOf(image.length()));
response.setHeader("Content-Disposition", "inline; filename=\"" + image.getName() + "\"");
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
input = new BufferedInputStream(new FileInputStream(image), DEFAULT_BUFFER_SIZE);
output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
} finally {
close(output);
close(input);
}
}
private static void close(Closeable resource) {
if (resource != null) {
try {
resource.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
图像没有HTTP访问但是只能作为访问一个文件,servlet将不得不打开图像文件,读入内容并将它们放在响应缓冲区中“....不知道我是否正确。并帮助我如何从服务器目录位置获取图像并为用户显示正确的图像。
道歉如果问题很模糊,很多谢谢你的建议:),会试试看 –