|
@@ -2,6 +2,7 @@ package com.diagbot.service.impl;
|
|
|
|
|
|
import com.diagbot.client.fastdfs.FastDFSClient;
|
|
|
import com.diagbot.client.fastdfs.FastDFSFile;
|
|
|
+import com.diagbot.dto.FileDTO;
|
|
|
import com.diagbot.exception.CommonException;
|
|
|
import com.diagbot.exception.ServiceErrorCode;
|
|
|
import com.diagbot.service.UploadService;
|
|
@@ -11,8 +12,6 @@ import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* @Description: 文件上传服务接口实现
|
|
@@ -23,8 +22,9 @@ import java.util.Map;
|
|
|
@Service
|
|
|
public class UploadServiceImpl implements UploadService {
|
|
|
@Override
|
|
|
- public Map<String, Object> singleFileUpload(MultipartFile file) {
|
|
|
- String path = "";
|
|
|
+ public FileDTO singleFileUpload(MultipartFile file) {
|
|
|
+ FileDTO fileDTO = new FileDTO();
|
|
|
+
|
|
|
if (file.isEmpty()) {
|
|
|
throw new CommonException(ServiceErrorCode.FILE_UPLOAD_ERROE, "文件不能为空");
|
|
|
}
|
|
@@ -33,39 +33,22 @@ public class UploadServiceImpl implements UploadService {
|
|
|
throw new CommonException(ServiceErrorCode.FILE_MAX_SIZE_LIMIT, "文件上传失败,超出大小限制1MB");
|
|
|
}
|
|
|
try {
|
|
|
- // Get the file and save it somewhere
|
|
|
- path = saveFile(file);
|
|
|
+ fileDTO = saveFile(file);
|
|
|
} catch (Exception e) {
|
|
|
log.error("文件上传失败", e);
|
|
|
throw new CommonException(ServiceErrorCode.FILE_UPLOAD_ERROE, "文件上传失败,请重新上传");
|
|
|
}
|
|
|
- return resultMap("SUCCESS", path, file.getOriginalFilename(), file.getOriginalFilename());
|
|
|
+ return fileDTO;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 上传文件返回格式
|
|
|
- *
|
|
|
- * @param state
|
|
|
- * @param url
|
|
|
- * @param title
|
|
|
- * @param original
|
|
|
- * @return
|
|
|
- */
|
|
|
- private Map<String, Object> resultMap(String state, String url, String title, String original) {
|
|
|
- Map<String, Object> result = new HashMap<>();
|
|
|
- result.put("state", state);
|
|
|
- result.put("original", original);
|
|
|
- result.put("title", title);
|
|
|
- result.put("url", url);
|
|
|
- return result;
|
|
|
- }
|
|
|
|
|
|
/**
|
|
|
* @param multipartFile
|
|
|
* @return
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
- public String saveFile(MultipartFile multipartFile) throws IOException {
|
|
|
+ public FileDTO saveFile(MultipartFile multipartFile) throws IOException {
|
|
|
+ FileDTO fileDTO = new FileDTO();
|
|
|
String[] fileAbsolutePath = {};
|
|
|
String fileName = multipartFile.getOriginalFilename();
|
|
|
String ext = fileName.substring(fileName.lastIndexOf(".") + 1);
|
|
@@ -89,7 +72,11 @@ public class UploadServiceImpl implements UploadService {
|
|
|
throw new CommonException(ServiceErrorCode.FILE_UPLOAD_ERROE, "文件上传失败,请重新上传");
|
|
|
}
|
|
|
String path = "/" + fileAbsolutePath[0] + "/" + fileAbsolutePath[1];
|
|
|
- return path;
|
|
|
+ fileDTO.setState("SUCCESS");
|
|
|
+ fileDTO.setUrl(path);
|
|
|
+ fileDTO.setOriginal(multipartFile.getOriginalFilename());
|
|
|
+ fileDTO.setTitle(multipartFile.getOriginalFilename());
|
|
|
+ return fileDTO;
|
|
|
|
|
|
}
|
|
|
|