美文网首页
zip格式下载

zip格式下载

作者: java_飞 | 来源:发表于2018-08-13 16:58 被阅读6次

注意引入zxing包

zxing依赖是为了生成二维码;

public void testEncode() throws WriterException, IOException {

int width = 200; // 图像宽度 int height = 200; // 图像高度 Map hints = new HashMap();

    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

    String temp_path="E:";

    File file = new File(temp_path);

    File zipFile = new File(temp_path+File.separator+"xx.zip");

    ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));

    for (int i = 0; i < 5; i++) {

        String content = "http://www.baidu.com";

        BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);// 生矩阵        BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);// 输出图像        ByteArrayOutputStream bs = new ByteArrayOutputStream();

        ImageOutputStream imOut = ImageIO.createImageOutputStream(bs);

        ImageIO.write(image, "jpg", imOut);

        InputStream is = new ByteArrayInputStream(bs.toByteArray());

        zipOut.putNextEntry(new ZipEntry( file.getName()+ File.separator +i+".jpg"));

        int temp = 0;

        while ((temp = is.read()) != -1) {

            zipOut.write(temp);

        }

        is.close();

    }

    zipOut.close();

}

相关文章

网友评论

      本文标题:zip格式下载

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