【问题标题】:Spring MVC display uploaded image into a External FolderSpring MVC 将上传的图像显示到外部文件夹中
【发布时间】:2023-03-03 00:26:01
【问题描述】:

我正在使用 Spring mvc 创建一个图像上传应用程序。我成功地将图像保存在外部文件夹中。保存图片的代码是这样的:

  MultipartFile productImage = product.getProductImage();

        path= Paths.get("/oms/images/"+product.getProductName()+".jpg");

        System.out.println(path.toString());
        if(productImage!=null && !productImage.isEmpty()){

            try {
                productImage.transferTo(new File(path.toString()));
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("Product Image Saving Failed ",e);

            }
        }

图像已成功保存到该外部文件夹中。但是当我尝试将相同的图像显示到 jsp 文件中时,我最终得到了这个错误....

http://localhost:8081/oms/images/Ss.jpg Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8081/oms/images/pup.jpg Failed to load resource: the server responded with a status of 404 (Not Found) 

显示图片的代码是这样的……

  <img src="<c:url value="/oms/images/${product.productName}.jpg"/>"/>

我看到了更多关于同一个问题的链接,但大多数问题是将图像保存到与 webapp 不同的目录中(显然)!

我错过了什么吗?如何将外部资源添加到 Spring 中?我应该怎么做才能将用户添加的图片显示到jsp页面中?

【问题讨论】:

    标签: jsp spring-mvc image-uploading


    【解决方案1】:

    上述解决方案对我不起作用。如果我尝试从 jsp 页面调用 @WebServlet("/FetchFile") ,则 @WebServlet("/FetchFile") 甚至都没有被调用。我谷歌了一下,找到了一个解决方案,在 spring mvc dispatcher-servlet.xml 中我们可以通过这种方式添加外部资源:

    <mvc:resources mapping="/images/**" location="file:///${fullPath}/oms/images/" />
    

    ${fullPath} 你应该在这里给出你的完整路径:

    然后从 jsp 页面你可以像这样调用 jsp 文件:

    <img src="/images/${product.productName}.jpg" alt="image"/>
    

    一整天后,我终于找到了解决方案,它对我有用。

    【讨论】:

      【解决方案2】:

      您可以使用 servlet 在 web 上下文之外检索图像,因为 jsp 代码只能在 web 上下文中工作。 举个例子

      @WebServlet("/FetchFile")
      public class FileServlet extends HttpServlet {
      
          @Override
          protected void doGet(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException
          {
              String filename = request.getParameter("productName");
              File file = new File("/oms/images/"+filename);
              response.setHeader("Content-Type", getServletContext().getMimeType(filename));
              response.setHeader("Content-Length", String.valueOf(file.length()));
              response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");
              Files.copy(file.toPath(), response.getOutputStream());
          }
      
      }
      

      并使用请求它

      <img scr="FetchFile?productName=${product.productName}.jpg">
      

      【讨论】:

      • 你的意思是我应该把这个方法添加到spring控制器然后从jsp页面调用这个方法?
      • 是的!这本来就是一个 ajax 调用
      • 我做了......我复制了类文件,我试图通过.. .... 但不适合我...我把 syso 也放在那个类文件中,看起来那个文件也没有被调用...怎么了?我也在使用 tomcat 7 和 tomcat 8 ...相同的结果.. @WebServlet("/FetchFile") is not getting invoked 可能是什么问题。我在浏览器控制台中没有收到任何错误。你能检查一下吗?
      • 谢谢@Rizstien .. 最后这对我有用...
      • 抱歉有一些问题可能是我无法查看您的完整评论。问题在于 servlet 映射技术,可能与您的配置不兼容。这个想法是制作一个 servlet 并从中下载资源
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-15
      • 2014-05-02
      • 1970-01-01
      • 2021-09-21
      • 1970-01-01
      • 2016-05-26
      • 1970-01-01
      相关资源
      最近更新 更多