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

  • 相关阅读:
    学习进度条40
    学习进度条39
    学习进度条38
    学习进度条37
    学习进度条36
    iReport5.6.0使用说明
    Mysql 如何创建一张临时表
    SQL语句出现sql关键字
    SQL-order by两个字段同时排序
    js中==和===区别
  • 原文地址:https://www.cnblogs.com/zuidongfeng/p/8859235.html
Copyright © 2011-2022 走看看