显示Image标签的SRC属性为Relative Path的图像

我已成功上传图片到

C:\Users\MyComputerName\Desktop\MyWorkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\MyWebApp\data 

并且图像名称示例lala.jpg保存在数据库中。

现在我试图在我的jsp中显示图像。

我发现我们需要创建一个servlet,它可以从Web容器外部加载文件,然后将/ stream文件写入您的响应。 其中一个例子是BalusC的File Servlet 。

我试图效仿,但我无法显示图像。

谁能指出我的错误? 帮助将是欣赏。 谢谢! 🙂

以下是我的代码..

JSP

 
Name Image
${staff.staffName}

文件Servlet中

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Get requested file by path info. String requestedFile = request.getParameter("path"); System.out.println(requestedFile); // Decode the file name (might contain spaces and on) and prepare file object. File file = new File(requestedFile); // Get content type by filename. String contentType = getServletContext().getMimeType(file.getName()); // If content type is unknown, then set the default value. // For all content types, see: http://www.w3schools.com/media/media_mimeref.asp // To add new content types, add new mime-mapping entry in web.xml. if (contentType == null) { contentType = "application/octet-stream"; } // Init servlet response. response.reset(); response.setBufferSize(DEFAULT_BUFFER_SIZE); response.setContentType(contentType); response.setHeader("Content-Length", String.valueOf(file.length())); response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\""); // Prepare streams. BufferedInputStream input = null; BufferedOutputStream output = null; try { // Open streams. input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE); output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE); // Write file contents to response. byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; int length; while ((length = input.read(buffer)) > 0) { output.write(buffer, 0, length); } } finally { // Gently close streams. close(output); close(input); } } // Helpers (can be refactored to public utility class) ---------------------------------------- private static void close(Closeable resource) { if (resource != null) { try { resource.close(); } catch (IOException e) { // Do your thing with the exception. Print it, log it or mail it. e.printStackTrace(); } } } 

web.xml中

   FileServlet FileServlet servlet.FileServlet   FileServlet /FileServlet  

正确的URL是:

 ">