springboot-为内置tomcat设置虚拟目录
2018-04-22 14:21
543 查看
需求
项目使用springboot开发,以jar包方式部署。项目中文件上传均保存到D判断下的upload目录下。
在浏览器中输入http://localhost:8080/upload/logo_1.jpg能访问到D盘upload目录下的logo_1.png图片
解决方法
由于使用jar包方式,无法使用为tomcat配置虚拟目录的方式,需为springboot内置tomcat设置虚拟目录。
实现
import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration public class WebMvcConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/upload/**").addResourceLocations("file:D:/upload/"); } }
启动项目,在浏览器中输入http://localhost:8080/upload/logo_1.jpg,即可正常访问图片了。
上面的例子中拦截器拦截的请求和文件上传目录均是写死的,可将其放置application.properties配置文件 中, 便于修改。修改后代码如下所示:
application.properties
server.port=8080 #静态资源对外暴露的访问路径 file.staticAccessPath=/upload/** #文件上传目录 file.uploadFolder=D:\\upload
FileUploadProperteis.java
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * 文件上传相关属性 * @create 2018-04-22 13:58 **/ @Component @ConfigurationProperties(prefix = "file") public class FileUploadProperteis { //静态资源对外暴露的访问路径 private String staticAccessPath; //文件上传目录 private String uploadFolder ; public String getStaticAccessPath() { return staticAccessPath; } public void setStaticAccessPath(String staticAccessPath) { this.staticAccessPath = staticAccessPath; } public String getUploadFolder() { return uploadFolder; } public void setUploadFolder(String uploadFolder) { this.uploadFolder = uploadFolder; } }
WebMvcConfig.java
import com.lnjecit.springboothelloworld.properties.FileUploadProperteis; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration public class WebMvcConfig extends WebMvcConfigurerAdapter { @Autowired private FileUploadProperteis fileUploadProperteis; @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler(fileUploadProperteis.getStaticAccessPath()).addResourceLocations("file:" + fileUploadProperteis.getUploadFolder() + "/"); } }
demo下载地址:https://github.com/linj6/springboot-learn/tree/master/springboot-helloworld
参考资料
1、https://liuyanzhao.com/7599.html
相关文章推荐
- spring boot内置tomcat设置虚拟目录
- Tomcat中虚拟目录的设置
- Tomcat 6.0 配置--虚拟目录的设置+多域名绑定
- tomcat中修改server.xml设置虚拟目录后,再删掉那部分内容,虚拟路径还是能访问的。
- Tomcat 6.0 虚拟目录设置
- Tomcat服务器虚拟目录的设置
- tomcat设置虚拟目录
- Tomcat虚拟目录设置
- Tomcat安装与环境配置之虚拟目录设置,内存参数设置
- 在Tomcat中设置虚拟目录
- Tomcat虚拟目录设置
- Tomcat设置虚拟目录的方法,不修改server.xml
- Tomcat设置虚拟目录
- Tomcat6设置虚拟目录和Server.xml详解
- Tomcat虚拟目录设置
- tomcat虚拟目录设置
- Tomcat 修改默认端口 以及Tomcat设置虚拟目录
- Tomcat虚拟目录设置
- Tomcat6.0配置(虚拟目录的设置+多域名绑定) 和tomcat 7 (转)
- tomcat5.0虚拟目录的设置