从网络获取多张二维码图片,压缩打包下载
项目最近有这个小需求,记录一下。
controller层:
1 /** 2 * 批量下载二维码图片 3 * 4 * @param deviceIdStr 5 */ 6 @RequestMapping("qr_code") 7 public void batchDownLoadQrCode(String deviceIdStr, HttpServletResponse response) throws Exception { 8 String[] deviceId = deviceIdStr.split(","); 9 List<Integer> deviceIds = new ArrayList<>(); 10 for (String id : deviceId) { 11 deviceIds.add(Integer.valueOf(id)); 12 } 13 DeviceQueryForm deviceQueryForm = new DeviceQueryForm(); 14 deviceQueryForm.setDeviceIds(deviceIds); 15 List<Device> deviceList = deviceClient.getDeviceList(deviceQueryForm); 16 17 // 生成zip文件 18 String name = "qrCode" + System.currentTimeMillis(); 19 String zipFileName = name + ".zip"; 20 String projectPath = System.getProperty("user.dir"); 21 String path = projectPath + File.separator + "qrCode"; 22 23 File file = new File(path); 24 //文件夹不存在则创建 25 if (!file.exists()) { 26 file.mkdirs(); 27 } 28 //新建一个zip 29 File zipFile = new File(path + File.separator + zipFileName); 30 ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile)); 31 32 for (Device device : deviceList) { 33 String imageName = device.getDeviceCode() + ".jpg"; 34 35 if (StringUtils.isBlank(device.getQrCodeUrl())) { 36 continue; 37 } 38 39 URL url = new URL(device.getQrCodeUrl()); 40 URLConnection conn = url.openConnection(); 41 conn.setConnectTimeout(3000); 42 InputStream inputStream = conn.getInputStream(); 43 File imageFile = FileUtil.generateImageFile(inputStream, path, imageName); 44 45 FileUtil.generateZip(zipOutputStream, imageFile); 46 47 //删除图片 48 if (imageFile.exists()) { 49 imageFile.delete(); 50 } 51 } 52 53 zipOutputStream.flush(); 54 zipOutputStream.close(); 55 56 //输出到客户端 57 OutputStream out = response.getOutputStream(); 58 response.reset(); 59 response.setHeader("Content-Disposition", "attachment;filename=" + zipFileName); 60 response.setContentType("application/zip;charset=utf-8"); 61 response.setCharacterEncoding("UTF-8"); 62 out.write(FileUtils.readFileToByteArray(zipFile)); 63 out.flush(); 64 out.close(); 65 //删除压缩包 66 if (zipFile.exists()) { 67 zipFile.delete(); 68 } 69 }
工具类:
1 package cn.ubox.mbss.ops.backend.utils; 2 3 import java.io.*; 4 import java.util.zip.ZipEntry; 5 import java.util.zip.ZipOutputStream; 6 7 /** 8 * @Author: 2YSP 9 * @Description: 10 * @Date: Created in 2017/12/13 11 */ 12 public class FileUtil { 13 /** 14 * 生成二维码图片 15 * @param inputStream 16 * @param path 17 * @param imageName 18 */ 19 public static File generateImageFile(InputStream inputStream,String path,String imageName){ 20 FileOutputStream outStream = null; 21 File imageFile = null; 22 BufferedInputStream bufferStream = null; 23 try{ 24 //创建二维码图片 25 imageFile = new File(path + File.separator + imageName); 26 27 outStream = new FileOutputStream(imageFile); 28 bufferStream = new BufferedInputStream(inputStream); 29 byte[] data = new byte[1024 * 100]; 30 int len = 0; 31 while ((len = bufferStream.read(data)) != -1) { 32 //写入数据 33 outStream.write(data, 0, len); 34 } 35 }catch (IOException e){ 36 e.printStackTrace(); 37 }finally { 38 if (outStream != null){ 39 try { 40 outStream.flush(); 41 outStream.close(); 42 } catch (IOException e) { 43 e.printStackTrace(); 44 } 45 } 46 if (bufferStream != null){ 47 try { 48 bufferStream.close(); 49 } catch (IOException e) { 50 e.printStackTrace(); 51 } 52 } 53 } 54 return imageFile; 55 } 56 57 /** 58 * 压缩图片为zip格式 59 * @param zipOutputStream 60 * @param imageFile 61 * 62 */ 63 public static void generateZip(ZipOutputStream zipOutputStream,File imageFile){ 64 BufferedInputStream bufferStream = null; 65 try { 66 InputStream input = new FileInputStream(imageFile); 67 String zipEntryName = imageFile.getName(); 68 byte[] bytes = new byte[1024 * 100];// 读写缓冲区 69 bufferStream = new BufferedInputStream(input);// 输入缓冲流 70 71 ZipEntry zipEntry = new ZipEntry(zipEntryName); 72 // 定位到该压缩条目位置,开始写入文件到压缩包中 73 zipOutputStream.putNextEntry(zipEntry); 74 int len = 0; 75 while ((len = bufferStream.read(bytes)) != -1) { 76 zipOutputStream.write(bytes, 0, len); 77 } 78 }catch (IOException e){ 79 e.printStackTrace(); 80 }finally { 81 if (bufferStream != null){ 82 try { 83 bufferStream.close(); 84 } catch (IOException e) { 85 e.printStackTrace(); 86 } 87 } 88 } 89 } 90 }
如果大家觉得有需要优化的地方,欢迎留言讨论。