• 二维码---生成并下载二维码


    package com.epalmpay.util;

    import com.google.zxing.BarcodeFormat;
    import com.google.zxing.EncodeHintType;
    import com.google.zxing.MultiFormatWriter;
    import com.google.zxing.WriterException;
    import com.google.zxing.client.j2se.MatrixToImageWriter;
    import com.google.zxing.common.BitMatrix;
    import org.apache.commons.io.output.ByteArrayOutputStream;
    import org.springframework.http.HttpHeaders;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.MediaType;
    import org.springframework.http.ResponseEntity;

    import javax.imageio.ImageIO;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;

    /**
    * Created by wucongpeng on 2017/6/11.
    */
    public class DownLoadUtil {

    /**
    * 生成并下载二维码
    * @param url 二维码对于URL
    * @param width 二维码宽
    * @param height 二维码高
    * @param format 二维码格式
    * @return
    * @throws WriterException
    * @throws IOException
    */
    public static ResponseEntity<byte[]> getResponseEntity(String url, String fileName, int width, int height, String format) throws WriterException, IOException {

    // String fileName = url.substring(url.lastIndexOf('/') + 1);

    Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    BitMatrix bitMatrix = new MultiFormatWriter().encode(url,
    BarcodeFormat.QR_CODE, width, height, hints);// 生成矩阵
    //将矩阵转为Image
    BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ImageIO.write(image, format, out);//将BufferedImage转成out输出流
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    headers.setContentDispositionFormData("attachment", new String(fileName.getBytes("utf-8"), "ISO8859-1"));
    return new ResponseEntity<byte[]>(out.toByteArray(),
    headers, HttpStatus.CREATED);
    }
    }
  • 相关阅读:
    Java入门:基础算法之从字符串中找到重复的字符
    Java入门:基础算法之产生随机数
    Java入门:基础算法之线性搜索
    Java入门:基础算法之检查奇偶性
    安装hadoop1.2.1集群环境
    Linux上安装JDK
    改变HTML中超链接的显示样式
    【Nutch2.2.1源代码分析之5】索引的基本流程
    【Nutch2.2.1源代码分析之4】Nutch加载配置文件的方法
    java生成UUID通用唯一识别码 (Universally Unique Identifier)
  • 原文地址:https://www.cnblogs.com/jabez1992/p/9222854.html
Copyright © 2020-2023  润新知