服务类
package com.sky.service;
import com.sky.constant.MessageConstant;
import com.sky.constant.PrefixConstant;
import com.sky.exception.BaseException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
@Service
public class CommonService {
@Value("${file-path.disk}")
private String disk;
@Value("${file-path.root-path}")
private String rootPath;
public String upload(String parentPath, MultipartFile file) {
String fileName = file.getOriginalFilename();
String fullPath = disk + ":" + File.separator + rootPath + File.separator + parentPath;
File directory = new File(fullPath);
if (!directory.exists()) {
directory.mkdirs();
}
File dest = new File(directory, fileName);
try {
file.transferTo(dest);
} catch (IOException e) {
throw new BaseException(MessageConstant.UPLOAD_FAILED);
}
return PrefixConstant.HTTP_PREFIX +
fileName;
}
}
配置文件

sky:
jwt:
admin-secret-key: itcast
admin-ttl: 720000000
admin-token-name: token
file-path:
disk: ${file-path.disk}
root-path: ${file-path.root-path}
file-path:
disk: D
root-path: sky_take_out
常量类

package com.sky.constant;
public class SystemPathConstant {
public static final String COMMON = "common";
private SystemPathConstant() {
}
}
package com.sky.constant;
public class PrefixConstant {
public static final String HTTP_PREFIX = "http://localhost/files/";
private PrefixConstant() {
}
}
controller层
@PostMapping("/upload")
@ApiOperation("文件上传")
public Result<String> upload(MultipartFile file) {
log.info("文件上传:{}", file);
String filePath = commonService.upload(SystemPathConstant.COMMON,file);
return Result.success(filePath);
}
配置类放行静态资源
package com.sky.config;
import com.sky.constant.SystemPathConstant;
import com.sky.interceptor.JwtTokenAdminInterceptor;
import com.sky.json.JacksonObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import java.util.List;
@Configuration
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {
@Autowired
private JwtTokenAdminInterceptor jwtTokenAdminInterceptor;
@Value("${file-path.disk}")
private String disk;
@Value("${file-path.root-path}")
private String rootPath;
..... 其他配置一万行
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
registry.addResourceHandler("/files/**")
.addResourceLocations("file:" +
disk + ":" + "/" +
rootPath + "/" +
SystemPathConstant.COMMON + "/");
}
}
nginx映射
location /files/ {
alias D:/sky_take_out/common/;
}