|
@@ -1,5 +1,6 @@
|
|
|
package com.fdkankan.site.service.impl;
|
|
|
|
|
|
+import com.fdkankan.filestorage.FileStorageTemplate;
|
|
|
import com.fdkankan.site.common.FilePath;
|
|
|
import com.fdkankan.site.common.ResultCode;
|
|
|
import com.fdkankan.site.common.util.UploadToOssUtil;
|
|
@@ -18,56 +19,57 @@ public class UploadService {
|
|
|
|
|
|
@Resource
|
|
|
private UploadToOssUtil uploadToOssUtil;
|
|
|
+ @Resource
|
|
|
+ FileStorageTemplate fileStorageTemplate;
|
|
|
|
|
|
- @Value("${upload.query-path}")
|
|
|
- private String queryPath;
|
|
|
|
|
|
public File uploadFile(MultipartFile file) {
|
|
|
- if(file.isEmpty()){
|
|
|
+ if (file.isEmpty()) {
|
|
|
throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
|
|
|
}
|
|
|
- if(file.getSize()>10 * 1024 * 1024 * 100){
|
|
|
+ if (file.getSize() > 10 * 1024 * 1024 * 100) {
|
|
|
System.out.println(file.getSize());
|
|
|
throw new BusinessException(ResultCode.UPLOAD_FILE_TO_LONG);
|
|
|
}
|
|
|
//获取文件名
|
|
|
String fileName = file.getOriginalFilename();
|
|
|
- if(StringUtils.isEmpty(fileName)){
|
|
|
+ if (StringUtils.isEmpty(fileName)) {
|
|
|
throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
|
|
|
}
|
|
|
File localFile = null;
|
|
|
try {
|
|
|
String suffixName = fileName.substring(fileName.lastIndexOf("."));
|
|
|
- fileName = UUID.randomUUID().toString().replace("-","");
|
|
|
- localFile = File.createTempFile(fileName,suffixName);
|
|
|
+ fileName = UUID.randomUUID().toString().replace("-", "");
|
|
|
+ localFile = File.createTempFile(fileName, suffixName);
|
|
|
file.transferTo(localFile);
|
|
|
return localFile;
|
|
|
- }catch (Exception e){
|
|
|
- e.printStackTrace();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
- public File uploadFile(MultipartFile file,String sceneCode,String type) {
|
|
|
- if(file.isEmpty()){
|
|
|
+
|
|
|
+ public File uploadFile(MultipartFile file, String sceneCode, String type) {
|
|
|
+ if (file.isEmpty()) {
|
|
|
throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
|
|
|
}
|
|
|
- if(file.getSize()>10 * 1024 * 1024 * 100){
|
|
|
+ if (file.getSize() > 10 * 1024 * 1024 * 100) {
|
|
|
System.out.println(file.getSize());
|
|
|
throw new BusinessException(ResultCode.UPLOAD_FILE_TO_LONG);
|
|
|
}
|
|
|
//获取文件名
|
|
|
String fileName = file.getOriginalFilename();
|
|
|
- if(StringUtils.isEmpty(fileName)){
|
|
|
+ if (StringUtils.isEmpty(fileName)) {
|
|
|
throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
|
|
|
}
|
|
|
File localFile = null;
|
|
|
try {
|
|
|
String suffixName = fileName.substring(fileName.lastIndexOf("."));
|
|
|
- fileName = UUID.randomUUID().toString().replace("-","");
|
|
|
- localFile = File.createTempFile(fileName,suffixName);
|
|
|
+ fileName = UUID.randomUUID().toString().replace("-", "");
|
|
|
+ localFile = File.createTempFile(fileName, suffixName);
|
|
|
file.transferTo(localFile);
|
|
|
return localFile;
|
|
|
- }catch (Exception e){
|
|
|
+ } catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return null;
|
|
@@ -75,9 +77,9 @@ public class UploadService {
|
|
|
|
|
|
public void deleteOssUrl(String path) {
|
|
|
try {
|
|
|
- String replace = path.replace(queryPath, "");
|
|
|
+ String replace = path.replace(fileStorageTemplate.calculateUrl(""), "");
|
|
|
uploadToOssUtil.delete(replace);
|
|
|
- }catch (Exception e){
|
|
|
+ } catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|