|
@@ -1,16 +1,24 @@
|
|
|
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.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.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
-
|
|
|
/**
|
|
|
* @Description: 文件上传服务接口实现
|
|
|
* @author: gaodm
|
|
@@ -19,6 +27,9 @@ import java.io.InputStream;
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
public class UploadServiceImpl implements UploadService {
|
|
|
+ @Value("${imageUrl.klm}")
|
|
|
+ private String imagerUrl;
|
|
|
+
|
|
|
@Override
|
|
|
public FileDTO singleFileUpload(MultipartFile file) {
|
|
|
if (file.isEmpty()) {
|
|
@@ -28,71 +39,90 @@ public class UploadServiceImpl implements UploadService {
|
|
|
if (file.getSize() > 1024 * 1024) {
|
|
|
return new FileDTO("FAILURE", "文件上传失败,超出大小限制1MB");
|
|
|
}
|
|
|
+
|
|
|
+ String result = "";
|
|
|
try {
|
|
|
- FileDTO fileDTO = saveFile(file);
|
|
|
- return fileDTO;
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("文件上传失败", e);
|
|
|
- return new FileDTO("FAILURE", "文件上传失败,请重新上传");
|
|
|
- }
|
|
|
- }
|
|
|
+ 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("output", "json")
|
|
|
+ .build();
|
|
|
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url(imagerUrl + "/group1/upload")
|
|
|
+ .post(multipartBody)
|
|
|
+ .build();
|
|
|
|
|
|
- /**
|
|
|
- * @param multipartFile
|
|
|
- * @return
|
|
|
- * @throws IOException
|
|
|
- */
|
|
|
- public FileDTO saveFile(MultipartFile multipartFile) throws IOException {
|
|
|
+ Response response = httpClient.newCall(request).execute();
|
|
|
+ if (response.isSuccessful()) {
|
|
|
+ ResponseBody body = response.body();
|
|
|
+ if (body != null) {
|
|
|
+ result = body.string();
|
|
|
+ //System.out.println(result);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- String[] fileAbsolutePath = {};
|
|
|
- String fileName = multipartFile.getOriginalFilename();
|
|
|
- String ext = fileName.substring(fileName.lastIndexOf(".") + 1);
|
|
|
- byte[] file_buff = null;
|
|
|
- InputStream inputStream = multipartFile.getInputStream();
|
|
|
- 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);
|
|
|
- try {
|
|
|
- fileAbsolutePath = FastDFSClient.upload(file); //upload to fastdfs
|
|
|
+ if (StringUtil.isBlank(result)) {
|
|
|
+ return new FileDTO("FAILURE", "文件上传失败,请重新上传");
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
|
- log.error("文件上传异常", e);
|
|
|
- return new FileDTO("FAILURE", "文件上传异常");
|
|
|
- }
|
|
|
- if (fileAbsolutePath == null) {
|
|
|
- log.error("文件上传失败,请重新上传");
|
|
|
+ log.error("文件上传失败", e);
|
|
|
return new FileDTO("FAILURE", "文件上传失败,请重新上传");
|
|
|
}
|
|
|
- String path = "/" + fileAbsolutePath[0] + "/" + fileAbsolutePath[1];
|
|
|
+
|
|
|
+ FileUploadDTO fileUploadDTO = GsonUtil.toObject(result, FileUploadDTO.class);
|
|
|
FileDTO fileDTO = new FileDTO("SUCCESS", "文件上传成功");
|
|
|
- fileDTO.setUrl(path);
|
|
|
- fileDTO.setOriginal(multipartFile.getOriginalFilename());
|
|
|
- fileDTO.setTitle(multipartFile.getOriginalFilename());
|
|
|
+ fileDTO.setUrl(fileUploadDTO.getPath());
|
|
|
+ fileDTO.setMd5(fileUploadDTO.getMd5());
|
|
|
+ fileDTO.setOriginal(file.getOriginalFilename());
|
|
|
+ fileDTO.setTitle(file.getOriginalFilename());
|
|
|
return fileDTO;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 删除服务端文件
|
|
|
*
|
|
|
- * @param path
|
|
|
+ * @param md5
|
|
|
* @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("/"));
|
|
|
+ public FileDTO deleteRemoteFile(String md5) {
|
|
|
+ String result = "";
|
|
|
try {
|
|
|
- FastDFSClient.deleteFile(groupName, fileName);
|
|
|
+ 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", "文件删除成功");
|
|
|
}
|
|
|
}
|