【问题标题】:(Explanation required) retrieve image from computer folder(需要说明)从计算机文件夹中检索图像
【发布时间】:2014-05-14 04:31:09
【问题描述】:

我一直在关注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 一直为空...谁能帮我检查代码....任何帮助将不胜感激!!!提前谢谢!!!

【问题讨论】:

  • 我真的一点都不懂...有没有你能帮我理解这个!!!
  • 所以你想在 servlet 中读取文件?
  • 是的!如果你能帮助我,我会很高兴!
  • image.jpg 的路径是什么??我的意思是真正的路径?
  • 首先我将图像上传到位于此处“C:\Users\Roshan Shahukhal\Documents\NetBeansProjects\E-Drivers Licensing System\build\web\images\”的图像文件夹中。 . 现在我想检索该图像并显示在视图页面中!

标签: java jsp servlets


【解决方案1】:

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

BalusC 解释得更好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);
        ...
}

【讨论】:

  • 感谢您分享您的项目!我会去看看结果如何!!!无论如何,再次感谢!
猜你喜欢
  • 2021-11-03
  • 1970-01-01
  • 2017-10-07
  • 1970-01-01
  • 1970-01-01
  • 2017-05-06
  • 2019-12-08
  • 2017-07-18
相关资源
最近更新 更多