标签:部署 class return xtend 无法 http dem rri world
项目使用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
标签:部署 class return xtend 无法 http dem rri world
原文地址:https://www.cnblogs.com/zuidongfeng/p/8859235.html