最近偶尔接收一个ssm+jsp的项目,因为之前的文件上传都是存在项目里,用tomcat启动的话,往往不能实时加载图片等资源
做一个用户头像切换功能,上传了文件后(上传是用的layui的组件,上传无问题)。但是加载的时候,必须重新启动项目才可以。我靠,这怎么行?
方法1:springmvc里springmvc.xml的配置
![](https://img-blog.csdnimg.cn/img_convert/e4cf158c1e28df209dc3b2d979568d94.png)
<mvc:resources location="file:///C:/upload/djw/**" mapping="/upload/"/>
这里的location就是对应的本地文件路径。mapping对应的就是请求路径,网上有人成功加载!
但是我也不行,怎么都不行!!!!
可以加载项目内的文件,配置有效,但是映射到本地不可以
方法2:tomcat里配置映射
server.xml里
![](https://img-blog.csdnimg.cn/img_convert/35d68bcf300f2f7ec78da6ee7e91650c.png)
<Context path="/upload" docBase="C:\upload\djw" reloadable="true" debug="0" crossContext="true"></Context>
然后在web.xml里配置
![](https://img-blog.csdnimg.cn/img_convert/54a734bb85eae80726219d9503a48c41.png)
这里的改为true,最后在
![](https://img-blog.csdnimg.cn/img_convert/263d6c89d3c0880a3959b51bfae3b4db.png)
最后测试
![](https://img-blog.csdnimg.cn/img_convert/7340d63a7470770b80c25aa4db3a4115.png)
成功运行啦!!!!!
上述是用的ssm框架,如果是springboot的话就简单太多了:就不用改任何tomcat东西。
在application.properties里配置
spring.mvc.static-path-pattern=/**
spring.resources.static-locations=classpath:/public/,classpath:/templates/,file:${uploadFile.basePath}
uploadFile.basePath=C:/upload/sthj
这个uploadFile.basePath=C:/upload/sthj就是我自己写的,命名和内容都是自己规定的。
完结完结!