package com.caimei.service.auth.impl; import com.aliyuncs.utils.StringUtils; import com.caimei.mapper.cmMapper.FileMapper; import com.caimei.model.en.QrCodeSize; import com.caimei.model.po.ProductImagePo; import com.caimei.model.po.UploadFilePo; import com.caimei.model.vo.AuthVo; import com.caimei.service.auth.DownloadService; import com.caimei.service.auth.UploadService; import com.caimei.utils.ImageUtils; import com.caimei.utils.OSSUtils; 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 javax.annotation.Resource; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; /** * Description * * @author : Aslee * @date : 2021/7/9 */ @Slf4j @Service public class DownloadServiceImpl implements DownloadService { @Resource private FileMapper fileMapper; private UploadService uploadService; @Value("${caimei.oldapi}") private String wwwServer; @Value("${caimei.zpapi}") private String zpServer; @Value("${spring.profiles.active}") private String active; @Autowired public void setUploadService(UploadService uploadService) { this.uploadService = uploadService; } @Override public void downloadFile(String ossName, String fileName, HttpServletRequest request, HttpServletResponse response) throws IOException { OSSUtils.downFile("authFile/", ossName, fileName); download(request, response, "./" + fileName, fileName); } @Override public void downloadShopImage(Integer authUserId, Integer type, HttpServletRequest request, HttpServletResponse response) throws Exception { if (null == authUserId || null == type) { return; } ArrayList fileList = new ArrayList<>(); List productImageList = fileMapper.getProductImageList(authUserId); List authImageList = fileMapper.getAuthImageList(authUserId); if (1 == type) { productImageList.forEach(productImage -> { UploadFilePo file = new UploadFilePo(); file.setFileUrl(productImage.getCertificateImage()); file.setFileName(productImage.getAuthParty() + "-" + productImage.getSnCode()); if (StringUtils.isNotEmpty(file.getFileUrl())) { fileList.add(file); } }); } else if (2 == type) { productImageList.forEach(productImage -> { String qrCodeLink = wwwServer + "/product/auth/product-" + productImage.getProductId() + ".html"; // 生成二维码 BufferedImage qrCodeImage = ImageUtils.createQrCode(qrCodeLink, QrCodeSize.XLARGE); String fileName = productImage.getAuthParty() + "-" + productImage.getSnCode(); String imagePath = createBufferedImageFile(qrCodeImage, fileName, "png"); UploadFilePo file = new UploadFilePo(); file.setFileUrl(imagePath); file.setFileName(productImage.getAuthParty() + "-" + productImage.getSnCode()); fileList.add(file); }); } else { List fileNameList = new ArrayList<>(); authImageList.forEach(authImage -> { String qrCodeLink = zpServer + "/" + authUserId + "/app/approve/club/detail?id=" + authImage.getAuthId(); // 生成二维码 BufferedImage qrCodeImage = ImageUtils.createQrCode(qrCodeLink, QrCodeSize.LARGE); String fileName = authImage.getAuthParty(); int k = 1; while (fileNameList.contains(fileName)) { fileName = fileName.replace("(" + (k-1) + ")", "") + "(" + k + ")"; k++; } fileNameList.add(fileName); String imagePath = createBufferedImageFile(qrCodeImage, fileName, "png"); UploadFilePo file = new UploadFilePo(); file.setFileUrl(imagePath); file.setFileName(authImage.getAuthParty()); fileList.add(file); }); } String fileName = 1 == type ? "授权牌" : 2 == type ? "设备二维码" : "机构二维码"; // 将授权牌压缩成zip文件 String imageZipPath = uploadService.createImageZip(fileList); // download(request, response, imageZipPath, fileName + ".zip"); } private String createBufferedImageFile(BufferedImage qrCodeImage, String fileName, String prefix) { String filePath = "/mnt/newdatadrive/data/runtime/jar-instance/zplma/tempFile/"; if ("dev".equals(active)){ filePath = "D:\\WorkSpace\\file\\tempImport\\"; } filePath = filePath + fileName + "." + prefix; File file = new File(filePath); try { ImageIO.write(qrCodeImage, prefix, file); } catch (Exception e) { e.printStackTrace(); } return filePath; } public void download(HttpServletRequest request, HttpServletResponse response, String filePath, String fileName) throws IOException { File file = new File(filePath); // 文件存在才下载 if (file.exists()) { OutputStream out = null; FileInputStream in = null; try { // 1.读取要下载的内容 in = new FileInputStream(file); // 2. 告诉浏览器下载的方式以及一些设置 // 解决文件名乱码问题,获取浏览器类型,转换对应文件名编码格式,IE要求文件名必须是utf-8, firefo要求是iso-8859-1编码 String agent = request.getHeader("user-agent"); if (agent.contains("FireFox")) { fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1"); } else { fileName = URLEncoder.encode(fileName, "UTF-8"); } // 设置下载文件的mineType,告诉浏览器下载文件类型 String mineType = request.getServletContext().getMimeType(fileName); response.setContentType(mineType); // 设置一个响应头,无论是否被浏览器解析,都下载 response.setHeader("Content-disposition", "attachment; filename=" + fileName); // 将要下载的文件内容通过输出流写到浏览器 out = response.getOutputStream(); int len = 0; byte[] buffer = new byte[1024]; while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } } catch (IOException e) { e.printStackTrace(); } finally { if (out != null) { out.close(); } if (in != null) { in.close(); } file.delete(); } } } }