有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java(需要解释)从计算机文件夹中检索图像

我一直在关注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) 个答案

  1. # 1 楼答案

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

    巴卢斯克解释得更好stackoverflow.com/a/8521981/660408

    我在eclipse项目目录中创建了“resources”文件夹,并将图像放在那里

    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);
            ...
    }