上传SpringMVC

时间:2015-09-27 09:42:49

标签: spring-mvc file-upload

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

我存储文件的路径是:

@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服务器上运行app的temp文件夹上有一个新文件夹附近的资源文件夹,如上所述(上传)。

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

但是在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中,我无法访问该图像。

所以任何通过像我这样的方法上传的本地图像文件的解决方案。

我一直在搜索,我没有看到任何解决方案来帮助我。

请帮助我!!

1 个答案:

答案 0 :(得分:0)

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

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