美文网首页
java导出压缩文件

java导出压缩文件

作者: LegendaryTao | 来源:发表于2019-12-12 10:13 被阅读0次

    一、controller

    package com.mydataway.code.controller;
    
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiImplicitParam;
    import io.swagger.annotations.ApiImplicitParams;
    import io.swagger.annotations.ApiOperation;
    import org.apache.commons.io.IOUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.util.LinkedHashMap;
    import java.util.List;
    
    @RestController
    @RequestMapping("/code")
    public class ChannelCodeController {
    
     /**
         * 导出压缩二维码
         */
        @GetMapping("/download")
        public void download(HttpServletResponse response, @RequestParam Integer[] ids) throws IOException {
            List<Integer> integerList = Arrays.asList(ids);
            ChannelCodeIdListDto channelCodeIdListDto = new ChannelCodeIdListDto();
            channelCodeIdListDto.setIdList(integerList);
    
            byte[] data = channelCodeService.download(channelCodeIdListDto);
            response.reset();
            response.setHeader("Content-Disposition", "attachment; filename=\"code.zip\"");
            response.addHeader("Content-Length", "" + data.length);
            response.setContentType("application/octet-stream; charset=UTF-8");
            IOUtils.write(data, response.getOutputStream());
        }
    }
    

    二、工具类

    package com.mydataway.coderecord;
    
    import lombok.extern.slf4j.Slf4j;
    import org.apache.commons.codec.binary.Base64;
    
    import javax.imageio.ImageIO;
    import java.awt.image.BufferedImage;
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.List;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipOutputStream;
    
    /**
     * 压缩导出工具
     */
    @Slf4j
    public class GenUtils {
    
        /**
         * @param base64Str       图片的base64
         * @param channelCodeName 图片名称
         * @param zip
         */
        public static void addZip(String base64Str, String channelCodeName, ZipOutputStream zip) {
            int length = base64Str.length();
            String base = base64Str.substring(22, length).replace("\r\n", "");
            try {
                zip.putNextEntry(new ZipEntry(channelCodeName + ".jpg"));
    
                byte[] src1 = Base64.decodeBase64(base);
                InputStream inputStream = new ByteArrayInputStream(src1);
                BufferedImage image = ImageIO.read(inputStream);
                ImageIO.write(image, "jpg", zip);
                zip.closeEntry();
            } catch (IOException e) {
                log.error("addZip-error", e);
                e.printStackTrace();
            }
        }
    
        /**
         * @param channelCodes
         * @return
         */
        public static byte[] addZipChannelCodes(List<ChannelExportDto> channelCodes) {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            try (
                    ZipOutputStream zip = new ZipOutputStream(outputStream)
            ) {
                for (ChannelExportDto channelCode : channelCodes) {
    
                    GenUtils.addZip(channelCode.getChannelCodePictureBase(), channelCode.getChannelCodeName(), zip);
                }
            } catch (IOException e) {
                log.error("addZipChannelCodes" + e);
            }
            return outputStream.toByteArray();
        }
    }
    

    相关文章

      网友评论

          本文标题:java导出压缩文件

          本文链接:https://www.haomeiwen.com/subject/vpfagctx.html