springboot 路径映像

图片链接示例
首先我们需要在application配置文件中设置以下内容:
在这里插入图片描述
这里是路径设置
在这里插入图片描述
其次我们需要编写一个封装类
在这里插入图片描述
最后我们还需要一个静态资源映像类
在这里插入图片描述
最后附上所有代码
WebMvcConfigurer.class

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 WebMvcConfigurer extends WebMvcConfigurerAdapter {
    @Autowired
    private File_init fileInit;
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //和页面有关的静态目录都放在项目的static目录下
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");

        registry.addResourceHandler("/apk/**").addResourceLocations("file:"+ fileInit.getUploadFolder_apk());
        //上传的图片在D盘下的OTA目录下,访问路径如:http://114.55.249.135:8888/touxiang/a.png
        //其中image_file表示访问的前缀。"file:/home/tomuser/apps/pinche/pas_card/"是文件真实的存储路径
        super.addResourceHandlers(registry);
    }
}

File_init.class

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * 路径映像
 */
@Component
@ConfigurationProperties(prefix = "file")
public class File_init {
    private String staticAccessPath_apk;//apk安装包
    private String uploadFolder_apk;

    public String getStaticAccessPath_apk() {
        return staticAccessPath_apk;
    }

    public void setStaticAccessPath_apk(String staticAccessPath_apk) {
        this.staticAccessPath_apk = staticAccessPath_apk;
    }

    public String getUploadFolder_apk() {
        return uploadFolder_apk;
    }

    public void setUploadFolder_apk(String uploadFolder_apk) {
        this.uploadFolder_apk = uploadFolder_apk;
    }
}

application.yml

server:
  port: 8089
#  servlet:
#    context-path: /photo

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/photo?serverTimezone=UTC&&characterEncoding=utf-8
    username: root
    password: root
  #静态资源访问
  resources:
    add-mappings: true
    static-locations: classpath\:/META-INF/resources/,classpath\:/resources/,classpath\:/static/,classpath\:/public/,file\:${file.truepath}${file.staticpath}

  servlet:
    multipart:
      #最大文件大小。值可以使用后缀“MB”或“KB”。指示兆字节或千字节大小。(-1为无限制)
      max-file-size: -1
      # 最大请求大小可以是mb也可以是kb
      max-request-size: -1


  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect

file:
  #静态资源对外暴露的访问路径
  #apk安装包
  staticAccessPath_apk: http://114.55.249.135:8089/apk/
  uploadFolder_apk: /home/tomuser/apps/photo/apk/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值