|
@@ -1,23 +1,33 @@
|
|
|
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.dto.FileDeleteDTO;
|
|
|
import com.diagbot.dto.FileThumDTO;
|
|
|
+import com.diagbot.dto.FileUploadDTO;
|
|
|
import com.diagbot.exception.CommonErrorCode;
|
|
|
import com.diagbot.exception.CommonException;
|
|
|
import com.diagbot.service.UploadService;
|
|
|
+import com.diagbot.util.GsonUtil;
|
|
|
+import com.diagbot.util.StringUtil;
|
|
|
+import io.github.lvyahui8.spring.aggregate.facade.DataBeanAggregateQueryFacade;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import net.coobird.thumbnailator.Thumbnails;
|
|
|
+import okhttp3.FormBody;
|
|
|
+import okhttp3.MediaType;
|
|
|
+import okhttp3.MultipartBody;
|
|
|
+import okhttp3.OkHttpClient;
|
|
|
+import okhttp3.Request;
|
|
|
+import okhttp3.RequestBody;
|
|
|
+import okhttp3.Response;
|
|
|
+import okhttp3.ResponseBody;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
-import java.io.ByteArrayInputStream;
|
|
|
-import java.io.ByteArrayOutputStream;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* @Description: 文件上传服务接口实现
|
|
@@ -27,215 +37,141 @@ import java.util.List;
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
public class UploadServiceImpl implements UploadService {
|
|
|
+ @Autowired
|
|
|
+ private DataBeanAggregateQueryFacade dataBeanAggregateQueryFacade;
|
|
|
+
|
|
|
+ @Value("${imageUrl.prefix}")
|
|
|
+ private String imagerUrl;
|
|
|
+
|
|
|
@Override
|
|
|
public FileDTO singleFileUpload(MultipartFile file) {
|
|
|
if (file.isEmpty()) {
|
|
|
return new FileDTO("FAILURE", "文件不能为空");
|
|
|
}
|
|
|
//文件大小上限4M
|
|
|
- if (file.getSize() > 4 * 1024 * 1024) {
|
|
|
+ if (file.getSize() > 1024 * 1024 * 4) {
|
|
|
return new FileDTO("FAILURE", "文件上传失败,超出大小限制4MB");
|
|
|
}
|
|
|
+
|
|
|
+ String result = "";
|
|
|
try {
|
|
|
- FileDTO fileDTO = saveFile(file, false);
|
|
|
- return fileDTO;
|
|
|
+ OkHttpClient httpClient = new OkHttpClient();
|
|
|
+ MultipartBody multipartBody = new MultipartBody.Builder().
|
|
|
+ setType(MultipartBody.FORM)
|
|
|
+ .addFormDataPart("file", file.getOriginalFilename(),
|
|
|
+ RequestBody.create(MediaType.parse("multipart/form-data;charset=utf-8"),
|
|
|
+ file.getBytes()))
|
|
|
+ .addFormDataPart("scene", "M01")
|
|
|
+ .addFormDataPart("output", "json")
|
|
|
+ .build();
|
|
|
+
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url(imagerUrl + "/group1/upload")
|
|
|
+ .post(multipartBody)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ Response response = httpClient.newCall(request).execute();
|
|
|
+ if (response.isSuccessful()) {
|
|
|
+ ResponseBody body = response.body();
|
|
|
+ if (body != null) {
|
|
|
+ result = body.string();
|
|
|
+ //System.out.println(result);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtil.isBlank(result)) {
|
|
|
+ return new FileDTO("FAILURE", "文件上传失败,请重新上传");
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
|
log.error("文件上传失败", e);
|
|
|
return new FileDTO("FAILURE", "文件上传失败,请重新上传");
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- @Override
|
|
|
- public FileThumDTO singleFileThumUpload(MultipartFile file){
|
|
|
- FileThumDTO fileThumDTO = new FileThumDTO();
|
|
|
- if (file.isEmpty()) {
|
|
|
- fileThumDTO.setSource(new FileDTO("FAILURE", "文件不能为空"));
|
|
|
- return fileThumDTO;
|
|
|
- }
|
|
|
- //文件大小上限4M
|
|
|
- if (file.getSize() > 4 * 1024 * 1024) {
|
|
|
- fileThumDTO.setSource(new FileDTO("FAILURE", "文件上传失败,超出大小限制4MB"));
|
|
|
- return fileThumDTO;
|
|
|
- }
|
|
|
- try {
|
|
|
- fileThumDTO = saveFileWithThum(file);
|
|
|
- return fileThumDTO;
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("文件上传失败", e);
|
|
|
- fileThumDTO.setSource(new FileDTO("FAILURE", "文件上传失败,请重新上传"));
|
|
|
- return fileThumDTO;
|
|
|
- }
|
|
|
+ FileUploadDTO fileUploadDTO = GsonUtil.toObject(result, FileUploadDTO.class);
|
|
|
+ FileDTO fileDTO = new FileDTO("SUCCESS", "文件上传成功");
|
|
|
+ fileDTO.setUrl(fileUploadDTO.getPath());
|
|
|
+ fileDTO.setMd5(fileUploadDTO.getMd5());
|
|
|
+ fileDTO.setOriginal(file.getOriginalFilename());
|
|
|
+ fileDTO.setTitle(file.getOriginalFilename());
|
|
|
+ return fileDTO;
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public List<FileDTO> multiFileUpload(MultipartFile[] mpfs) {
|
|
|
- // 上传文件返回的路径集合
|
|
|
- List<FileDTO> fileDTOS = new ArrayList<>();
|
|
|
- if (null == mpfs) {
|
|
|
- throw new CommonException(CommonErrorCode.PARAM_IS_NULL, "文件不能为空");
|
|
|
- }
|
|
|
-
|
|
|
- for (MultipartFile file : mpfs) {
|
|
|
- if (file.isEmpty()) {
|
|
|
- fileDTOS.add(new FileDTO("FAILURE", "文件不能为空"));
|
|
|
- continue;
|
|
|
- }
|
|
|
- //文件大小上限4M
|
|
|
- if (file.getSize() > 4 * 1024 * 1024) {
|
|
|
- fileDTOS.add(new FileDTO("FAILURE", "文件上传失败,超出大小限制4MB"));
|
|
|
- continue;
|
|
|
+ /**
|
|
|
+ * 删除服务端文件
|
|
|
+ *
|
|
|
+ * @param md5
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public FileDTO deleteRemoteFile(String md5) {
|
|
|
+ String result = "";
|
|
|
+ try {
|
|
|
+ OkHttpClient httpClient = new OkHttpClient();
|
|
|
+ RequestBody formBody = new FormBody.Builder()
|
|
|
+ .add("md5", md5)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url(imagerUrl + "/group1/delete")
|
|
|
+ .post(formBody)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ Response response = httpClient.newCall(request).execute();
|
|
|
+ if (response.isSuccessful()) {
|
|
|
+ ResponseBody body = response.body();
|
|
|
+ if (body != null) {
|
|
|
+ result = body.string();
|
|
|
+ //System.out.println(result);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- try {
|
|
|
- FileDTO fileDTO = saveFile(file, false);
|
|
|
- fileDTOS.add(fileDTO);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("文件上传失败", e);
|
|
|
- fileDTOS.add(new FileDTO("FAILURE", "文件上传失败,请重新上传"));
|
|
|
+ if (StringUtil.isBlank(result)) {
|
|
|
+ return new FileDTO("FAILURE", "文件删除失败");
|
|
|
}
|
|
|
- }
|
|
|
- return fileDTOS;
|
|
|
- }
|
|
|
|
|
|
-
|
|
|
- @Override
|
|
|
- public List<FileThumDTO> multiFileThumUpload(MultipartFile[] mpfs, Integer[] type) {
|
|
|
- // 上传文件返回的路径集合
|
|
|
- List<FileThumDTO> fileDTOS = new ArrayList<>();
|
|
|
- if (null == mpfs) {
|
|
|
- throw new CommonException(CommonErrorCode.PARAM_IS_NULL, "文件不能为空");
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("", e);
|
|
|
+ return new FileDTO("FAILURE", "文件删除失败");
|
|
|
}
|
|
|
|
|
|
- FileThumDTO fileThumDTO = null;
|
|
|
- for (int i = 0; i < mpfs.length; i++) {
|
|
|
- MultipartFile file = mpfs[i];
|
|
|
- if (file.isEmpty()) {
|
|
|
- fileThumDTO = new FileThumDTO();
|
|
|
- fileThumDTO.setSource(new FileDTO("FAILURE", "文件不能为空", type[i]));
|
|
|
- fileDTOS.add(fileThumDTO);
|
|
|
- continue;
|
|
|
- }
|
|
|
- //文件大小上限4M
|
|
|
- if (file.getSize() > 4 * 1024 * 1024) {
|
|
|
- fileThumDTO = new FileThumDTO();
|
|
|
- fileThumDTO.setSource(new FileDTO("FAILURE", "文件上传失败,超出大小限制4MB", type[i]));
|
|
|
- fileDTOS.add(fileThumDTO);
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- fileThumDTO = saveFileWithThum(file);
|
|
|
- fileThumDTO.getSource().setType(type[i]);
|
|
|
- fileThumDTO.getThumbnail().setType(type[i]);
|
|
|
- fileDTOS.add(fileThumDTO);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("文件上传失败", e);
|
|
|
- fileThumDTO = new FileThumDTO();
|
|
|
- fileThumDTO.setSource(new FileDTO("FAILURE", "文件上传失败,请重新上传", type[i]));
|
|
|
- fileDTOS.add(fileThumDTO);
|
|
|
- }
|
|
|
+ FileDeleteDTO fileDeleteDTO = GsonUtil.toObject(result, FileDeleteDTO.class);
|
|
|
+ if (fileDeleteDTO.getStatus().equals("fail")) {
|
|
|
+ return new FileDTO("FAILURE", fileDeleteDTO.getMessage());
|
|
|
}
|
|
|
- return fileDTOS;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @param multipartFile
|
|
|
- * @return
|
|
|
- * @throws IOException
|
|
|
- */
|
|
|
- public FileThumDTO saveFileWithThum(MultipartFile multipartFile) throws IOException {
|
|
|
- FileThumDTO fileThumDTO = new FileThumDTO();
|
|
|
- //原图上传到服务器
|
|
|
- FileDTO source = saveFile(multipartFile);
|
|
|
- fileThumDTO.setSource(source);
|
|
|
- //压缩图片
|
|
|
- FileDTO thumbnail = saveFile(multipartFile, true);
|
|
|
- fileThumDTO.setThumbnail(thumbnail);
|
|
|
- return fileThumDTO;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @param multipartFile
|
|
|
- * @return
|
|
|
- * @throws IOException
|
|
|
- */
|
|
|
- public FileDTO saveFile(MultipartFile multipartFile) throws IOException {
|
|
|
- return saveFile(multipartFile, false);
|
|
|
+ return new FileDTO("SUCCESS", "文件删除成功");
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * @param multipartFile
|
|
|
- * @return
|
|
|
- * @throws IOException
|
|
|
- */
|
|
|
- public FileDTO saveFile(MultipartFile multipartFile, Boolean isThum) throws IOException {
|
|
|
- String[] fileAbsolutePath = {};
|
|
|
- String fileName = multipartFile.getOriginalFilename();
|
|
|
- String ext = fileName.substring(fileName.lastIndexOf(".") + 1);
|
|
|
- byte[] file_buff = null;
|
|
|
- InputStream inputStream = multipartFile.getInputStream();
|
|
|
- //压缩图片
|
|
|
- if (isThum) {
|
|
|
- Integer width = 280;
|
|
|
- Integer hight = 430;
|
|
|
- //文件名重新命名
|
|
|
- fileName = width + "_" + hight + "_" + fileName;
|
|
|
- inputStream = compress(inputStream, width, hight);
|
|
|
- }
|
|
|
|
|
|
- if (inputStream != null) {
|
|
|
- int len1 = inputStream.available();
|
|
|
- file_buff = new byte[len1];
|
|
|
- inputStream.read(file_buff);
|
|
|
- }
|
|
|
- inputStream.close();
|
|
|
- FastDFSFile file = new FastDFSFile(fileName, file_buff, ext);
|
|
|
+ @Override
|
|
|
+ public List<FileDTO> multiFileThumUpload(MultipartFile[] mpfs, Integer[] type) {
|
|
|
+ // 上传文件返回的路径集合
|
|
|
+ List<FileDTO> fileDTOS = new ArrayList<>();
|
|
|
try {
|
|
|
- fileAbsolutePath = FastDFSClient.upload(file); //upload to fastdfs
|
|
|
+ Map<String, Object> invokeParams = new HashMap<>();
|
|
|
+ for (int i = 0; i < 6; i++) {
|
|
|
+ int j = i + 1;
|
|
|
+ String fileName = "file" + j;
|
|
|
+ String typeName = "type" + j;
|
|
|
+ if (null != mpfs && null != type) {
|
|
|
+ if (i < mpfs.length && i < type.length
|
|
|
+ && null != mpfs[i] && null != type[i]) {
|
|
|
+ invokeParams.put(fileName, mpfs[i]);
|
|
|
+ invokeParams.put(typeName, type[i]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ fileDTOS
|
|
|
+ = dataBeanAggregateQueryFacade.get("thumUploads", invokeParams, List.class);
|
|
|
} catch (Exception e) {
|
|
|
- log.error("文件上传异常", e);
|
|
|
- return new FileDTO("FAILURE", "文件上传异常");
|
|
|
+ throw new CommonException(CommonErrorCode.SERVER_IS_ERROR);
|
|
|
}
|
|
|
- if (fileAbsolutePath == null) {
|
|
|
- log.error("文件上传失败,请重新上传");
|
|
|
- return new FileDTO("FAILURE", "文件上传失败,请重新上传");
|
|
|
- }
|
|
|
- String path = "/" + fileAbsolutePath[0] + "/" + fileAbsolutePath[1];
|
|
|
- FileDTO fileDTO = new FileDTO("SUCCESS", "文件上传成功");
|
|
|
- fileDTO.setUrl(path);
|
|
|
- fileDTO.setOriginal(fileName);
|
|
|
- fileDTO.setTitle(fileName);
|
|
|
- return fileDTO;
|
|
|
-
|
|
|
+ return fileDTOS;
|
|
|
}
|
|
|
|
|
|
- public ByteArrayInputStream compress(InputStream inputStream, Integer width, Integer hight) throws IOException {
|
|
|
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
- Thumbnails.of(inputStream)
|
|
|
- .size(width, hight)
|
|
|
- .toOutputStream(baos);
|
|
|
- final ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
|
|
|
- return swapStream;
|
|
|
- }
|
|
|
|
|
|
- /**
|
|
|
- * 删除服务端文件
|
|
|
- *
|
|
|
- * @param path
|
|
|
- * @return
|
|
|
- */
|
|
|
- public FileDTO deleteRemoteFile(String path) {
|
|
|
- if (path.startsWith("/")) {
|
|
|
- path = path.substring(1);
|
|
|
- }
|
|
|
- String fileName = path.substring(path.indexOf("/") + 1);
|
|
|
- String groupName = path.substring(0, path.indexOf("/"));
|
|
|
- try {
|
|
|
- FastDFSClient.deleteFile(groupName, fileName);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("", e);
|
|
|
- return new FileDTO("FAILURE", "文件删除失败");
|
|
|
- }
|
|
|
- return new FileDTO("SUCCESS", "文件删除成功");
|
|
|
+ @Override
|
|
|
+ public FileDTO singleFileUpload(MultipartFile file, Integer type) {
|
|
|
+ FileDTO fileDTO = singleFileUpload(file);
|
|
|
+ fileDTO.setType(type);
|
|
|
+ return fileDTO;
|
|
|
}
|
|
|
}
|