需求

项目使用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

springboot-为内置tomcat设置虚拟目录的更多相关文章

  1. Spring boot内置Tomcat的临时目录被删除导致文件上传不了-问题解析

    目录 1.问题 2.1. 为什么需要使用这个/tmp/tomcat*? 2.2.那个 /tmp/tomcat* 目录为什么不存在? 三.解决办法 修改 springboot 配置,不要在/tmp 下创 ...

  2. springboot禁用内置Tomcat的不安全请求方法

    起因:安全组针对接口测试提出的要求,需要关闭不安全的请求方法,例如put.delete等方法,防止服务端资源被恶意篡改. 用过springMvc都知道可以使用@PostMapping.@GetMapp ...

  3. 模拟Springboot二:内置tomcat

    既然要将tomcat内置到项目中,并且能够成功的启动项目就要知道 tomcat  做了哪些事情 ,那么就必须先搞明白 一个 普通的web项目是如何被我们本地配置的tomcat启动并运行的 (1). 先 ...

  4. Tomcat设置虚拟目录的方法, 不修改server.xm

    所在小组使用的就是这样的形式开发,这样切换开发环境,测试环境,正式环境就只需要修改project.xml文件就行了.project.xml命名是随意的,访问的时候就使用这个名字来访问. 在tomcat ...

  5. springboot让内置tomcat失效

    一.POM(去除内嵌tomcat后,需要添加servlet依赖) <dependency> <groupId>org.springframework.boot</grou ...

  6. springboot内置tomcat配置虚拟路径

    在Springboot中默认的静态资源路径有:classpath:/METAINF/resources/,classpath:/resources/,classpath:/static/,classp ...

  7. 如何优雅的使用springboot项目内置tomcat

    问题:以前,我们在使用SSM框架的时候,都是通过外置的tomcat进行部署,如果想访问文件,直接拖到项目的根目录下面即可.假如我们需要放一个apk文件,然后让别人下载,只需将apk放到项目根目录下面, ...

  8. tomcat设置虚拟目录开启文件下载在服务

    因为平时一直在eclipse里运行tomcat,所以改的文件在eclipse里的server 在server.xml里找到<host></host>,并在</host&g ...

  9. Spring Boot2.0之 原理—创建内置Tomcat容器

    前面所述的https://www.cnblogs.com/toov5/p/9823728.html 中的第一条先不赘述了,就是玩了maven 重点介绍后两条 首先内置Tomcat: SpringBoo ...

随机推荐

  1. Paper Reading - Learning like a Child: Fast Novel Visual Concept Learning from Sentence Descriptions of Images ( ICCV 2015 )

    Link of the Paper: https://arxiv.org/pdf/1504.06692.pdf Innovations: The authors propose the Novel V ...

  2. geth账户密码

    xiaocong geth账户密码 123 {d6abe909013d8da914ae2a08c9b58e7b76601b39} 账户密码 123456 0x4A7F15104F54dB3214D2F ...

  3. windows编程常见数据类型

    windows编程常见数据类型, 总结一下方便查阅: 类型 对应指针 描述 ATOM . typedef WORD ATOM; BOOL LPBOOL 布尔类型,值要写成TRUE或FALSE,实际上是 ...

  4. Daily Scrum8

    今天我们小组开会内容分为以下部分: part 1: 说明了search.match.upload.download功能实现流程: part 2:针对用户积分模块的任务分配: ◆Part 1 searc ...

  5. Delegate(QLabel和QComboBox)

    一.最终效果 二.实现思路 1.createEditor()中create两个控件,分别是QLabel和QComboBox,将其添加到一个widget中,然后返回该widget: 2.setEdito ...

  6. Java 动态绑定和多态

    动态绑定和多态 动态绑定是指:"在执行程序期间(而非编译期间),判断引用所指对象的实际类型,调用其相应的方法." 动态绑定(多态)存在的条件 要有继承. 要有重写. 父类引用指向子 ...

  7. Java中I/O流之Object流

    Java 中的 object 流:直接将 Object 对象写入或读出 1. serializable 接口:序列化,可以被序列化的,若确实需要将某个类的对象写在硬盘上或网络上,想把他们序列化成一个字 ...

  8. “小葵日记”API接口文档

    "小葵日记"项目API接口文档 时间:2017/10/31 (1)用户登录[待完成] POST:127.0.0.1/index/user/login data 数据别称 数据名 数 ...

  9. <Effective C++>读书摘要--Ctors、Dtors and Assignment Operators<一>

    <Item 5> Know what functions C++ silently writes and calls 1.If you don't declare them yoursel ...

  10. 解决ueditor编辑器图片在线管理图片无法显示

    使用ueditor,点击在线管理,服务器图片路径显示不正确,如下图所示  查看源码,如下:  发现图片src中中间多了一长串的项目跟路径,解决的办法是 把  jsp/controller.jsp 里面 ...