package com.caimei.service.auth.impl; import com.alibaba.fastjson.JSONArray; import com.caimei.config.FastDfsClient; import com.caimei.model.po.UploadFilePo; import com.caimei.service.auth.UploadService; import lombok.extern.slf4j.Slf4j; 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.*; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.UUID; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; /** * Description * * @author : Aslee * @date : 2021/5/11 */ @Slf4j @Service public class UploadServiceImpl implements UploadService { @Autowired private FastDfsClient client; @Value("${spring.profiles.active}") private String active; @Value("${caimei.imageDomain}") private String imageDomain; @Override public String saveFile(MultipartFile file) throws IOException { String originalFilename = file.getOriginalFilename(); String randomStr = UUID.randomUUID().toString(); assert originalFilename != null; int index = originalFilename.lastIndexOf("."); String extName = originalFilename.substring(index); String filePath = "/mnt/newdatadrive/data/runtime/jar-instance/zplma/tempFile/"; if ("dev".equals(active)){ filePath = "D:\\WorkSpace\\file\\tempImport\\"; } filePath += randomStr + extName; // 临时图片 File tempFile = new File(filePath); file.transferTo(tempFile); log.info("【图片上传】>>>>>>>>>>>>>>>>图片临时路径:" + filePath); String imageUrl = imageDomain + "/" + client.uploadFile(filePath); // 删除临时图片 boolean delete = tempFile.delete(); log.info("【图片上传】>>>>>>>>>>>>>>>>删除临时图片:" + delete); return imageUrl; } @Override public String saveImageZip(JSONArray imageArr) throws Exception { ArrayList fileList = new ArrayList<>(); for (Object object : imageArr) { UploadFilePo file = new UploadFilePo(); file.setFileUrl(object.toString()); String imgRandomId = UUID.randomUUID().toString(); file.setFileName(imgRandomId); fileList.add(file); } String imageZipPath = createImageZip(fileList); // 上传zip String imageUrl = imageDomain + "/" + client.uploadFile(imageZipPath); // 删除临时图片 File zipFile = new File(imageZipPath); boolean delete = zipFile.delete(); log.info("【图片上传】>>>>>>>>>>>>>>>>删除临时图片压缩包:" + delete); return imageUrl; } @Override public String createImageZip(List fileList) throws Exception { String randomStr = UUID.randomUUID().toString(); String filePath = "/mnt/newdatadrive/data/runtime/jar-instance/zplma/tempImage/"; if ("dev".equals(active)){ filePath = "D:\\uploadZip\\"; } filePath += randomStr + ".zip"; File zipFile = new File(filePath); log.info("【图片上传】>>>>>>>>>>>>>>>>图片压缩包临时路径:" + filePath); ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile)); BufferedOutputStream bo = new BufferedOutputStream(out); for (UploadFilePo file : fileList) { String fileUrl = file.getFileUrl(); File image = null; if (fileUrl.startsWith("http")) { HttpURLConnection httpUrl = (HttpURLConnection) new URL(fileUrl).openConnection(); httpUrl.connect(); String prefix = fileUrl.substring(fileUrl.lastIndexOf(".")); image = inputStreamToFile(httpUrl.getInputStream(), file.getFileName() + "." + prefix); } else { image = new File(fileUrl); } out.putNextEntry(new ZipEntry(image.getName())); FileInputStream in = new FileInputStream(image); BufferedInputStream bi = new BufferedInputStream(in); int b; while ((b = bi.read()) != -1) { bo.write(b); } bo.flush(); bi.close(); in.close(); image.delete(); } bo.close(); out.close(); return filePath; } @Override public String saveFileByUrl(String url,String fileName) throws Exception { InputStream input = new URL(url).openStream(); File file = inputStreamToFile(input, fileName); String filePath = System.getProperty("java.io.tmpdir") + File.separator + fileName; log.info("【图片上传】>>>>>>>>>>>>>>>>图片临时路径:" + filePath); String imageUrl = imageDomain + "/" + client.uploadFile(filePath); // 删除临时图片 boolean delete = file.delete(); log.info("【图片上传】>>>>>>>>>>>>>>>>删除临时图片:" + delete); return imageUrl; } /** * 工具类 * inputStream 转 File */ public static File inputStreamToFile(InputStream ins, String name) throws Exception{ File file = new File(System.getProperty("java.io.tmpdir") + File.separator + name); if (file.exists()) { return file; } OutputStream os = new FileOutputStream(file); int bytesRead; int len = 8192; byte[] buffer = new byte[len]; while ((bytesRead = ins.read(buffer, 0, len)) != -1) { os.write(buffer, 0, bytesRead); } os.close(); ins.close(); return file; } }