UploadServiceImpl.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package com.caimei.service.auth.impl;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.caimei.config.FastDfsClient;
  4. import com.caimei.model.po.UploadFilePo;
  5. import com.caimei.service.auth.UploadService;
  6. import lombok.extern.slf4j.Slf4j;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.beans.factory.annotation.Value;
  9. import org.springframework.stereotype.Service;
  10. import org.springframework.web.multipart.MultipartFile;
  11. import java.io.*;
  12. import java.net.HttpURLConnection;
  13. import java.net.URL;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. import java.util.UUID;
  17. import java.util.zip.ZipEntry;
  18. import java.util.zip.ZipOutputStream;
  19. /**
  20. * Description
  21. *
  22. * @author : Aslee
  23. * @date : 2021/5/11
  24. */
  25. @Slf4j
  26. @Service
  27. public class UploadServiceImpl implements UploadService {
  28. @Autowired
  29. private FastDfsClient client;
  30. @Value("${spring.profiles.active}")
  31. private String active;
  32. @Value("${caimei.imageDomain}")
  33. private String imageDomain;
  34. @Override
  35. public String saveFile(MultipartFile file) throws IOException {
  36. String originalFilename = file.getOriginalFilename();
  37. String randomStr = UUID.randomUUID().toString();
  38. assert originalFilename != null;
  39. int index = originalFilename.lastIndexOf(".");
  40. String extName = originalFilename.substring(index);
  41. String filePath = "/mnt/newdatadrive/data/runtime/jar-instance/zplma/tempFile/";
  42. if ("dev".equals(active)){
  43. filePath = "D:\\WorkSpace\\file\\tempImport\\";
  44. }
  45. filePath += randomStr + extName;
  46. // 临时图片
  47. File tempFile = new File(filePath);
  48. file.transferTo(tempFile);
  49. log.info("【图片上传】>>>>>>>>>>>>>>>>图片临时路径:" + filePath);
  50. String imageUrl = imageDomain + "/" + client.uploadFile(filePath);
  51. // 删除临时图片
  52. boolean delete = tempFile.delete();
  53. log.info("【图片上传】>>>>>>>>>>>>>>>>删除临时图片:" + delete);
  54. return imageUrl;
  55. }
  56. @Override
  57. public String saveImageZip(JSONArray imageArr) throws Exception {
  58. ArrayList<UploadFilePo> fileList = new ArrayList<>();
  59. for (Object object : imageArr) {
  60. UploadFilePo file = new UploadFilePo();
  61. file.setFileUrl(object.toString());
  62. String imgRandomId = UUID.randomUUID().toString();
  63. file.setFileName(imgRandomId);
  64. fileList.add(file);
  65. }
  66. String imageZipPath = createImageZip(fileList);
  67. // 上传zip
  68. String imageUrl = imageDomain + "/" + client.uploadFile(imageZipPath);
  69. // 删除临时图片
  70. File zipFile = new File(imageZipPath);
  71. boolean delete = zipFile.delete();
  72. log.info("【图片上传】>>>>>>>>>>>>>>>>删除临时图片压缩包:" + delete);
  73. return imageUrl;
  74. }
  75. @Override
  76. public String createImageZip(List<UploadFilePo> fileList) throws Exception {
  77. String randomStr = UUID.randomUUID().toString();
  78. String filePath = "/mnt/newdatadrive/data/runtime/jar-instance/zplma/tempImage/";
  79. if ("dev".equals(active)){
  80. filePath = "D:\\uploadZip\\";
  81. }
  82. filePath += randomStr + ".zip";
  83. File zipFile = new File(filePath);
  84. log.info("【图片上传】>>>>>>>>>>>>>>>>图片压缩包临时路径:" + filePath);
  85. ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));
  86. BufferedOutputStream bo = new BufferedOutputStream(out);
  87. for (UploadFilePo file : fileList) {
  88. String fileUrl = file.getFileUrl();
  89. File image = null;
  90. if (fileUrl.startsWith("http")) {
  91. HttpURLConnection httpUrl = (HttpURLConnection) new URL(fileUrl).openConnection();
  92. httpUrl.connect();
  93. String prefix = fileUrl.substring(fileUrl.lastIndexOf("."));
  94. image = inputStreamToFile(httpUrl.getInputStream(), file.getFileName() + "." + prefix);
  95. } else {
  96. image = new File(fileUrl);
  97. }
  98. out.putNextEntry(new ZipEntry(image.getName()));
  99. FileInputStream in = new FileInputStream(image);
  100. BufferedInputStream bi = new BufferedInputStream(in);
  101. int b;
  102. while ((b = bi.read()) != -1) {
  103. bo.write(b);
  104. }
  105. bo.flush();
  106. bi.close();
  107. in.close();
  108. image.delete();
  109. }
  110. bo.close();
  111. out.close();
  112. return filePath;
  113. }
  114. @Override
  115. public String saveFileByUrl(String url,String fileName) throws Exception {
  116. InputStream input = new URL(url).openStream();
  117. File file = inputStreamToFile(input, fileName);
  118. String filePath = System.getProperty("java.io.tmpdir") + File.separator + fileName;
  119. log.info("【图片上传】>>>>>>>>>>>>>>>>图片临时路径:" + filePath);
  120. String imageUrl = imageDomain + "/" + client.uploadFile(filePath);
  121. // 删除临时图片
  122. boolean delete = file.delete();
  123. log.info("【图片上传】>>>>>>>>>>>>>>>>删除临时图片:" + delete);
  124. return imageUrl;
  125. }
  126. /**
  127. * 工具类
  128. * inputStream 转 File
  129. */
  130. public static File inputStreamToFile(InputStream ins, String name) throws Exception{
  131. File file = new File(System.getProperty("java.io.tmpdir") + File.separator + name);
  132. if (file.exists()) {
  133. return file;
  134. }
  135. OutputStream os = new FileOutputStream(file);
  136. int bytesRead;
  137. int len = 8192;
  138. byte[] buffer = new byte[len];
  139. while ((bytesRead = ins.read(buffer, 0, len)) != -1) {
  140. os.write(buffer, 0, bytesRead);
  141. }
  142. os.close();
  143. ins.close();
  144. return file;
  145. }
  146. }