文件上传到服务器本地,然后预览图片
接口
@Value("${file.winFilePath}")
private String winFilePath;
@Value("${file.linuxFilePath}")
private String linuxFilePath;
@Value("${file.fileIp}")
private String fileIp;
@Value("${file.filePort}")
private String filePort;
public String localUpload(MultipartFile file) {
if (file == null) {
throw new RuntimeException("文件不能为空");
}
//后缀
String extension = "." + FilenameUtils.getExtension(file.getOriginalFilename());
String cruDate = DateUtil.format(new Date(), "yyyyMMdd");
String uuid = UUID.randomUUID().toString();
String newFileNamePrefix = cruDate + uuid;
//新文件名
String newFileName = newFileNamePrefix + extension;
//判断jar包所在的服务器
String oss = System.getProperty("os.name").toLowerCase();
String uploadPath = "";
if (oss.startsWith("win")) {
uploadPath= winFilePath;
} else {
uploadPath=linuxFilePath;
}
String dateDirPath = uploadPath + "/" + cruDate;
System.out.println(dateDirPath);
File dir = new File(dateDirPath);
if (!dir.exists()) {
dir.mkdirs();
}
try {
file.transferTo(new File(dir, newFileName));
} catch (IOException e) {
e.printStackTrace();
}
return fileIp+":"+filePort + "/upload/"+cruDate+"/"+newFileName;
}
WebConfiguration拦截
package com.chanlian.canteen.admin.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.io.File;
@Configuration
@Slf4j
public class WebConfiguration implements WebMvcConfigurer {
@Value("${file.winFilePath}")
private String winFilePath;
@Value("${file.linuxFilePath}")
private String linuxFilePath;
@Value("${file.mappingUrl}")
private String mappingUrl;
/**
* 允许跨域
* @param registry
*/
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
.allowCredentials(true)
.maxAge(3600)
.allowedHeaders("*");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
String uploadUrl=null;
String oss = System.getProperty("os.name").toLowerCase();
if (oss.startsWith("win")) {
uploadUrl= winFilePath;
} else {
uploadUrl=linuxFilePath;
}
registry.addResourceHandler(mappingUrl+"/**").addResourceLocations("file:"+ uploadUrl + File.separator);
System.out.println("file:"+ uploadUrl + File.separator);
}
}
接口放行
**放行/upload/接口
{
// 注册 Sa-Token 拦截器,定义详细认证规则
registry.addInterceptor(new SaInterceptor(handler -> {
// 指定一条 match 规则
SaRouter
.match("/**") // 拦截的 path 列表,可以写多个 */
// .notMatch("/**") // 拦截的 path 列表,可以写多个 */
.notMatch("/login/password") // 排除掉的 path 列表,可以写多个
.notMatch("/isLogin") // 排除掉的 path 列表,可以写多个
.notMatch("/upload/**") // 排除掉的 path 列表,可以写多个
.notMatch("**/swagger-ui.html",
"/swagger-resources/**",
"/webjars/**",
"/v2/**",
"/swagger-ui.html/**",
"/doc.html/**",
"/error",
"/favicon.ico",
"sso/auth",
"/csrf") // 排除掉的 path 列表,可以写多个
.check(r -> StpUtil.checkLogin()); // 要执行的校验动作,可以写完整的 lambda 表达式
})).addPathPatterns("/**");
}
上传文件接口返回,直接请求返回地址即可访问(本机测试所以返回127.0.0.1):
实际上传地址:
D:\file\20231204