문제

I have successfully upload an image to

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

And the image name example lala.jpg is saved in database.

Now I am trying to display the image in my jsp.

I found that we need to create a servlet that can load file from outside of your web container and then write/stream file to your response. One of the example is File Servlet by BalusC.

I tried to follow but I did not able to display the image.

Can anyone point out my mistakes? Help will be appreciate. Thanks! :)

Below are my codes..

jsp

<table>
<thead>
<tr>
<th>Name</th>
<th>Image</th>
</tr>
</thead>
<tbody>

<c:forEach items="${staff}" var="staff">
<tr>
<td>${staff.staffName}</td>
<td><img src="FileServlet?path=C:\Users\MyComputerName\Desktop\MyWorkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\MyWebApp\data\${staff.staffImage}"></td>
</tr>
</c:forEach>
</tbody>
</table>

In File 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();
            }
        }
    }

In web.xml

  <servlet>
    <display-name>FileServlet</display-name>
    <servlet-name>FileServlet</servlet-name>
    <servlet-class>servlet.FileServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>FileServlet</servlet-name>
    <url-pattern>/FileServlet</url-pattern>
  </servlet-mapping>
도움이 되었습니까?

해결책

The right URL is:

<img src="FileServlet?path=C:\Users\MyComputerName\Desktop\MyWorkspace\.metadata\.plug‌​ins\org.eclipse.wst.server.core\tmp0\wtpwebapps\MyWebApp\data\\<c:out value="${staff.staffImage}"/>">
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top