1.在properties文件設置圖片上傳的最大值:
# 圖片上傳最大值
spring.servlet.multipart.max-file-size=3MB
spring.servlet.multipart.max-request-size=3MB
2.在properties文件配置文件訪問地址:
# 圖片上傳真實路徑
picture.upload.url=D:/workspace/upload/picture/
# url訪問路徑
spring.mvc.static-path-pattern=/picture/**
# 圖片存放真是路徑
spring.resources.static-locations=file:D://workspace/upload/picture/
注意Linux和Windows上的目錄結構不同
# 圖片路徑
picture.upload.url=/root/uploadFiles
# url訪問路徑
spring.mvc.static-path-pattern=/picture/**
# 圖片存放真是路徑
spring.resources.static-locations=file://root/uploadFiles(該配置方法適用於圖片上傳地址和項目部署地址是同一臺服務器,沒有使用文件服務器,如果是文件服務器則用ftp方式去上傳,文章不做舉例)
@Value("${picture.upload.url}")
private String uploadUrl;
private static final String CONNECTURL = "picture";
上傳controller
@Override
@PostMapping(value = "/file/upload", consumes = "multipart/form-data")
public Result<String> fileUpload(MultipartFile file, String programId, String frameId, HttpServletRequest request) {
if (StringUtils.isEmpty(programId) || StringUtils.isEmpty(frameId) || file.isEmpty()) {
return Result.setResult(MessgeCode.PARAMETER_EMPTY);
}
String path = StringUtils.isEmpty(request.getContextPath()) ? "" : request.getContextPath() + "/";
// 服務器訪問地址
String ipAndPortUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/" + path;
String originNmae = file.getOriginalFilename();
String fileName = frameId + originNmae.substring(originNmae.indexOf("."));//生成圖片名稱
String realUrl = uploadUrl + programId + "/" + frameId + "/";
try {
FileUtil.uploadMethod(realUrl, fileName, file);
} catch (Exception e) {
e.printStackTrace();
return Result.setResult(MessgeCode.UPLOAD_FAIL);
}
String url = ipAndPortUrl + CONNECTURL + "/" + programId + "/" + frameId + "/" + fileName;
logger.info("訪問路徑" + url);
return Result.setResult(MessgeCode.OK, url);
}
public class FileUtil {
public static void uploadMethod(String fileUrl, String fileName, MultipartFile file) {
if (!file.isEmpty()) {
try {
File filepath = new File(fileUrl);
if (!filepath.exists()) {
filepath.mkdirs();
}
// 文件保存路徑
String savePath = fileUrl + fileName;
// 轉存文件
file.transferTo(new File(savePath));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
返回結果;http://187.33.2.2:8081/picture/11/110/110.png.
圖片訪問不需要JWT訪問權限可以修改配置類:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity.csrf() // 由於使用的是JWT,我們這裏不需要csrf
.disable().sessionManagement()// 基於token,所以不需要session
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
//暫時不需要登錄 權限 TODO
.antMatchers("/login/**").permitAll()
.antMatchers("/picture/**").permitAll() // picture/**對應配置文件裏面設置的映射地址
.anyRequest().authenticated();
// 禁用緩存
httpSecurity.headers().cacheControl();
// 添加JWT filter
httpSecurity.addFilterBefore(jwtAuthenticationTokenFilter(), UsernamePasswordAuthenticationFilter.class);
//添加自定義未授權和未登錄結果返回
httpSecurity.exceptionHandling()
.accessDeniedHandler(restfulAccessDeniedHandler)
.authenticationEntryPoint(restAuthenticationEntryPoint);
}
}