(需要说明)从计算机文件夹中检索图像

时间:2014-04-02 17:47:09

标签: java jsp servlets

我一直关注http://balusc.blogspot.in/2007/07/fileservlet.html,但我的代码无法正确使用,因为我对此并不了解...

filePath = request.getServletContext().getRealPath("")+File.separator+"images"+File.separator+"photo.jpg";
    System.out.println(filePath);
    // Get requested file by path info.
    String requestedFile = request.getPathInfo();
    System.out.println(requestedFile);
    // Check if file is actually supplied to the request URI.
    if (requestedFile == null) {
        // Do your thing if the file is not supplied to the request URI.
        // Throw an exception, or send 404, or show default/warning page, or just ignore it.
        response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
        System.out.println("terminates");
        return;
    }

我得到的是程序在这里终止,因为requestedFile一直是null ...任何人都可以帮助我完成代码....任何帮助将不胜感激!在此先感谢!!!

1 个答案:

答案 0 :(得分:3)

我在 hackathon 上写了这段代码,所以它不干净。您可以看到示例here,更具体地说是here

BalusC更好地解释了stackoverflow.com/a/8521981/660408

我创建了资源' eclipse项目目录中的文件夹,并在那里放置图像。

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String filename = request.getPathInfo().substring(1);
        File file = new File("D:\\FreeUni-enterprise\\CloudLibrary\\resources", filename);
        ...
}