zoukankan      html  css  js  c++  java
  • springboot-为内置tomcat设置虚拟目录

    需求

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

  • 相关阅读:
    tile38 复制配置
    The Guardian’s Migration from MongoDB to PostgreSQL on Amazon RDS
    tile38 一款开源的geo 数据库
    sqler sql 转rest api 的docker 镜像构建(续)使用源码编译
    sqler sql 转rest api javascript 试用
    sqler sql 转rest api redis 接口使用
    sqler sql 转rest api 的docker image
    sqler sql 转rest api 的工具试用
    apache geode 试用
    benthos v1 的一些新功能
  • 原文地址:https://www.cnblogs.com/zuidongfeng/p/8859235.html
Copyright © 2011-2022 走看看