原由
在实际的开发中,有可能我们会上传文件到服务器,然后返回一个url直接访问这个文件。但是当文件比较多,体积也比较大时,不可能将这些文件全部存放在jar服务中。这个时候,这种方式就特别有用,因为我们可以把磁盘上的一个位置映射为静态资源访问路径,通过访问静态资源路径就可以直接访问到磁盘上的资源. 而已 如果服务器运行jar包 就不可以新增填一些图片 文件夹等等 所以麻烦 保存到服务器的相应位置上面去
yml 配置
#文件上传路径
#/home/xxx
#文件上传绝对路径(注意:结尾“/”)
#file.upload.imgPath=D://file/upload/=> /home/xxx/upload/
webapp:
data:
access-path: /upload
path-pattern: ${webapp.data.access-path}/**
mapping-location-windows: D:/upload/
mapping-location-not-windows: /home/xxx/upload/
config 配置
package com.yqs.config.file;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.File;
/**
*
* * @Description 本地资源配置
* * @Author 小乌龟
* * @Date 2022/2/22 9:08
*
* 作用: 用于配置静态资源映射的一些属性
* accessPath: 访问静态资源时的路径,如访问ip:port/visual/upload/xxx/xxx,其中visual是项目路径,upload便是accessPath
* mappingLocationWindows: 指明在windows下运行时accessPath映射到服务器磁盘的绝对路径,比如映射到D:/data
* mappingLocationNotWindows: 指明在Linux或Mac下运行时accessPath映射到服务器磁盘的绝对路径,比如映射到/usr/data
* pathPattern: 配置静态资源映射时的参数,表示路径匹配规则,如/upload/**表示以ip:port/visual/upload/开头的url
* 都会认为是访问静态资源,然后到mappingLocation指定的路径下去寻找资源
* resourceLocation: 配置静态资源映射时的参数,表示磁盘的绝对路径,如file:/usr/upload/,必须加上file。
*/
@Data
@Component
public class WebAppDataProperties {
@Value("${webapp.data.access-path}")
private String accessPath;
@Value("${webapp.data.path-pattern}")
private String pathPattern;
@Value("${webapp.data.mapping-location-windows}")
private String mappingLocationWindows;
@Value("${webapp.data.mapping-location-not-windows}")
private String mapperLocationNotWindows;
public String getMappingLocation() {
String os = System.getProperty("os.name");
return (os.toLowerCase().startsWith("win")) ? mappingLocationWindows : mapperLocationNotWindows;
}
public String getResourceLocation() {
String os = System.getProperty("os.name");
if (os.toLowerCase().startsWith("win")) {
File file = new File(mappingLocationWindows);
if (!file.exists()) {
file.mkdirs();
}
return "file:" + mappingLocationWindows;
} else {
File file = new File(mapperLocationNotWindows);
if (!file.exists()) {
file.mkdirs();
}
return "file:" + mapperLocationNotWindows;
}
}
}
package com.yqs.config.file;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @Description 自定义静态资源映射
* @Author 小乌龟
* @Date 2022/2/18 16:13
*/
@Slf4j
@Configuration //这里特别要注意一下(@Configuration) 一定要加上 要不然springboot识别不了
public class FileUploadConfig implements WebMvcConfigurer {
@Autowired
private WebAppDataProperties webAppDataProperties;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(webAppDataProperties.getPathPattern())
.addResourceLocations(webAppDataProperties.getResourceLocation());
}
}
然后就可以运行测试运行成果了