如何将图像文件对象从java显示到jsp?

时间:2014-12-03 03:46:59

标签: java html image jsp servlets

我有一个返回图像文件对象的方法:

public File getPhoto(entryId){...}

我从我的action方法中调用此方法,并将文件设置为DTO File变量:

    myDto.photo = getPhoto(entryId); 
       // where entryId refers to the name of the image file 
       // e.g. ent01 for ent01.gif, ent02 for ent02.gif and so on.

现在,在我的JSP文件中,我想通过这样的代码显示图像:

<img src = "${myDto.photo}"> 

但是,我意识到myDto.photo是一个文件对象,因此具有文件的绝对路径,而不是JSP中img src所需的URL。

通过搜索,我知道我可以使用servlet并使用类似

的内容
<img src = "${pageContext.request.contextPath}/image/ent01.gif"}. 

但是,我对这个有点困惑,因为我希望文件名部分(ent01.gif)根据输入entryId而变化。

我希望有人能在这个问题上为我揭光。非常感谢。

1 个答案:

答案 0 :(得分:0)

您可以创建一个控制器类,以便显示所需的图像。

@Controller
public class ImageReadFile{

    // this is for mapping your image related path. 
    @RequestMapping(value="/image/*")
    public void readImage(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        ServletContext sc = request.getServletContext();

        //here i uploaded my image in this path
        // You can set any path here
        String imagePath = "/home/somefolder/Workspaces/Images/";
        String [] fragmentFilename = request.getServletPath().split("/");

        //Check if image isn't set
        if(fragmentFilename.length <= 2){
            return;
        }

        String filename = fragmentFilename[2];
        String requestedImage = "/"+filename;

        if(filename == null){
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        File image = new File(imagePath, URLDecoder.decode(requestedImage, "UTF-8"));

        if(!image.exists()){
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        String contentType = sc.getMimeType(image.getName());
        response.reset();
        response.setContentType(contentType);
        response.setHeader("Content-Length", String.valueOf(image.length()));
        Files.copy(image.toPath(), response.getOutputStream());
    }

}

Servlet版本。

试试这个。

@WebServlet("/image/*")
public class ImageWriter extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        ServletContext sc = request.getServletContext();

        //here i uploaded my image in this path
        // You can set any path here
        String imagePath = "/home/somefolder/Workspaces/Images/";
        String [] fragmentFilename = request.getServletPath().split("/");

        //Check if image isn't set
        if(fragmentFilename.length <= 2){
            return;
        }

        String filename = fragmentFilename[2];
        String requestedImage = "/"+filename;

        if(filename == null){
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        File image = new File(imagePath, URLDecoder.decode(requestedImage, "UTF-8"));

        if(!image.exists()){
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        String contentType = sc.getMimeType(image.getName());
        response.reset();
        response.setContentType(contentType);
        response.setHeader("Content-Length", String.valueOf(image.length()));
        Files.copy(image.toPath(), response.getOutputStream());

    }

}

这就是你要在jsp中设置它的方式,

<img alt="${imageFilename}" src="${pageContext.request.contextPath}/image/${imageFilename}">

只需将文件名传递给jsp,然后让控制器读取并显示它。

希望这会对你有所帮助。