【问题标题】:Cannot access to image file(s) after upload SpringMVC上传Spring MVC后无法访问图像文件)
【发布时间】:2015-09-27 19:36:48
【问题描述】:

使用spring-mvc上传后访问资源时卡住了。

我存储文件的路径是:

@Autowired
ServletContext context;
....
private File path(String fileName){
    return new File(context.getRealPath("/upload/images/")+fileName);
}
...//mkdirs before uploading the file(s)

所以在调用 mkdirs() 方法之后,在 apache-tomcat 服务器中运行应用程序的临时文件夹上,在资源文件夹附近有一个新文件夹,如上所示(上传)。

我创建的上传方法已成功将文件上传到该文件夹​​。 我可以使用与上述相同的解决方案访问该文件。

但是在 jsp 文件中,我无法访问我上传到该路径的图像文件。 请看下面的代码:

<%File item = (File)request.getAttribute("imageFile");%>
...//some conditions to validate the file
<img class="myprev-img" src="upload/images/<%=item.getName() %>" width="100%" height="100%">

我无法通过上面的代码获取图像,但是在普通的jsp和servlet应用程序中,该代码可以从本地读取图像。但是在 SpringMVC 中我无法访问该图像。

所以任何解决方案都可以像我一样通过相同的方法从本地上传访问图像文件。

我已经四处搜索,但没有看到任何解决方案可以帮助我解决这个问题。

请帮帮我!!

【问题讨论】:

    标签: spring-mvc file-upload


    【解决方案1】:

    我找到了解决问题的方法。请参阅以下链接:

    http://www.baeldung.com/spring-mvc-static-resources
    

    【讨论】:

      猜你喜欢
      • 2012-02-29
      • 2017-05-14
      • 2012-09-14
      • 1970-01-01
      • 2015-05-23
      • 2021-08-24
      • 2015-11-09
      • 2016-04-23
      • 1970-01-01
      相关资源
      最近更新 更多