|
@@ -11,6 +11,7 @@ import org.springframework.web.multipart.MultipartFile;
|
|
|
import javax.annotation.Resource;
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
+import java.util.LinkedHashSet;
|
|
|
import java.util.UUID;
|
|
|
|
|
|
@Service
|
|
@@ -24,7 +25,7 @@ public class UploadService {
|
|
|
@Value("${upload.query-path}")
|
|
|
private String queryPath;
|
|
|
|
|
|
- public String uploadFile(MultipartFile file, boolean newName, String filePathAdd) throws IOException {
|
|
|
+ public String uploadFile(MultipartFile file, boolean newName, String filePathAdd, LinkedHashSet<String> filePath) {
|
|
|
if(file.isEmpty()){
|
|
|
throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
|
|
|
}
|
|
@@ -37,21 +38,34 @@ public class UploadService {
|
|
|
if(StringUtils.isEmpty(fileName)){
|
|
|
throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
|
|
|
}
|
|
|
- //获取文件后缀名
|
|
|
- String suffixName = fileName.substring(fileName.lastIndexOf("."));
|
|
|
- //重新生成文件名
|
|
|
- if(newName){
|
|
|
- fileName = UUID.randomUUID().toString().replace("-","") + suffixName;
|
|
|
- }else {
|
|
|
- fileName= fileName.substring(0,fileName.lastIndexOf("."));
|
|
|
- }
|
|
|
- File localFile = File.createTempFile(fileName,suffixName);
|
|
|
- file.transferTo(localFile);
|
|
|
- String path = localFile.getPath();
|
|
|
- uploadToOssUtil.upload(path,filePath +filePathAdd+ fileName + suffixName);
|
|
|
- if(!uploadToOssUtil.existKey(filePath +filePathAdd + fileName + suffixName)){
|
|
|
+ File localFile = null;
|
|
|
+ try {
|
|
|
+ //获取文件后缀名
|
|
|
+ String suffixName = fileName.substring(fileName.lastIndexOf("."));
|
|
|
+ //重新生成文件名
|
|
|
+ if(newName){
|
|
|
+ fileName = UUID.randomUUID().toString().replace("-","") + suffixName;
|
|
|
+ }else {
|
|
|
+ fileName= fileName.substring(0,fileName.lastIndexOf("."));
|
|
|
+ }
|
|
|
+ localFile = File.createTempFile(fileName,suffixName);
|
|
|
+ file.transferTo(localFile);
|
|
|
+ String path = localFile.getPath();
|
|
|
+ if(filePath !=null){
|
|
|
+ filePath.add(path);
|
|
|
+ }
|
|
|
+ uploadToOssUtil.upload(path,filePath +filePathAdd+ fileName + suffixName);
|
|
|
+ if(!uploadToOssUtil.existKey(filePath +filePathAdd + fileName + suffixName)){
|
|
|
+ throw new BusinessException(ResultCode.UPLOAD_ERROR.code,ResultCode.UPLOAD_ERROR.msg);
|
|
|
+ }
|
|
|
+ return queryPath + filePath +filePathAdd+ fileName + suffixName;
|
|
|
+ }catch (Exception e){
|
|
|
throw new BusinessException(ResultCode.UPLOAD_ERROR.code,ResultCode.UPLOAD_ERROR.msg);
|
|
|
+ }finally {
|
|
|
+ if(localFile!=null){
|
|
|
+ localFile.deleteOnExit(); //删除临时文件
|
|
|
+ }
|
|
|
}
|
|
|
- return queryPath + filePath +filePathAdd+ fileName + suffixName;
|
|
|
+
|
|
|
}
|
|
|
}
|