|
@@ -0,0 +1,134 @@
|
|
|
+package com.diagbot.service.impl;
|
|
|
+
|
|
|
+import com.diagbot.dto.FileDTO;
|
|
|
+import com.diagbot.dto.FileDeleteDTO;
|
|
|
+import com.diagbot.dto.FileUploadDTO;
|
|
|
+import com.diagbot.service.UploadService;
|
|
|
+import com.diagbot.util.GsonUtil;
|
|
|
+import com.diagbot.util.StringUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+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.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Description: 文件上传服务接口实现
|
|
|
+ * @author: gaodm
|
|
|
+ * @time: 2018/11/13 13:50
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class UploadServiceImpl implements UploadService {
|
|
|
+ @Value("${imageUrl.prefix}")
|
|
|
+ private String imagerUrl;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public FileDTO singleFileUpload(MultipartFile file) {
|
|
|
+ if (file.isEmpty()) {
|
|
|
+ return new FileDTO("FAILURE", "文件不能为空");
|
|
|
+ }
|
|
|
+ //文件大小上限500M
|
|
|
+ if (file.getSize() > 1024 * 1024 * 500) {
|
|
|
+ return new FileDTO("FAILURE", "文件上传失败,超出大小限制500MB");
|
|
|
+ }
|
|
|
+
|
|
|
+ String result = "";
|
|
|
+ try {
|
|
|
+ OkHttpClient httpClient = new OkHttpClient.Builder()
|
|
|
+ .connectTimeout(500, TimeUnit.SECONDS)
|
|
|
+ .readTimeout(500, TimeUnit.SECONDS)
|
|
|
+ .build();
|
|
|
+ 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", "M05")
|
|
|
+ .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", "文件上传失败,请重新上传");
|
|
|
+ }
|
|
|
+
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除服务端文件
|
|
|
+ *
|
|
|
+ * @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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtil.isBlank(result)) {
|
|
|
+ return new FileDTO("FAILURE", "文件删除失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("", e);
|
|
|
+ return new FileDTO("FAILURE", "文件删除失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ FileDeleteDTO fileDeleteDTO = GsonUtil.toObject(result, FileDeleteDTO.class);
|
|
|
+ if (fileDeleteDTO.getStatus().equals("fail")) {
|
|
|
+ return new FileDTO("FAILURE", fileDeleteDTO.getMessage());
|
|
|
+ }
|
|
|
+ return new FileDTO("SUCCESS", "文件删除成功");
|
|
|
+ }
|
|
|
+}
|