2011-09-01 84 views
0

我正在处理用户信息被添加和修改(更新)的应用程序。使用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将不得不打开图像文件,读入内容并将它们放在响应缓冲区中“....不知道我是否正确。并帮助我如何从服务器目录位置获取图像并为用户显示正确的图像。

回答

1

我很难理解具体问题,但我相信你的根本问题是你不知道如何设置imagePath?它有一个错误的值。该代码显示它应该被设置为放置所有图像的根文件夹。在底层操作系统平台中,您需要将//some-location-ip address/D drive/images映射为Windows资源管理器中的网络驱动器,例如, Z:然后在您的imagePath中使用它。

this.imagePath = "Z:"; 

它也期望图像文件名作为请求pathinfo。因此,假设你的servlet被映射在/images/*的URL模式,那么你的<img src>应该基本上是这样的

<img src="images/filename.png" /> 

你也可以动态地EL填充它。例如。与登录用户的唯一的用户名:

<img src="images/${user.name}.png" /> 

至于使用"D:\\photo_not_available_large.png"替换图像,你可以设置当File#exists()回报false

+0

道歉如果问题很模糊,很多谢谢你的建议:),会试试看 –