图片链接示例
首先我们需要在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/