生成二维码图片并下载

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_30123829/article/details/82218594

简单业务说明:生成图片(此为生成二维码图片)之后,以流方式输出。

代码如下:

/**
 * 下载二维码,二维码存放查询当前学时记录的链接
 *@description 
 *@author ****
 *@date 2018-8-22下午2:17:06
 * @return
 */
public String downPersonQrCode(){
    UserSession us = (UserSession)getSession().getAttribute(SsoConstant.SSO_USER_SESSION_KEY);
    String qrCodeLink = "http://*******.action?credentialcode="+ us.getLoginId() + "&searchType=QrCode";
    //生成二维码接口
    BufferedImage img = Encrypt.commonQRCode(qrCodeLink, "png", 7);
    ByteArrayOutputStream os=new ByteArrayOutputStream();//新建流。
    OutputStream out = null;
    try {
        //图片存放到输出流里面
        ImageIO.write(img, "png", os);
        HttpServletResponse response = this.getResponse();
        out = response.getOutputStream();
        response.reset();
        response.setContentType("image/png");
        response.addHeader("Content-Disposition","attachment;filename=\""+us.getLoginId()+".png\"");
        response.setHeader("Content_Length",String.valueOf(os.size()));
        if (os != null) {
            out = new BufferedOutputStream(response.getOutputStream());
            byte by[] = os.toByteArray();
            out.write(by);
            out.flush();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if(os != null){
            try {
                os.close();
            }
             catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(out!=null){
            try {
                out.close();
            }
             catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return NONE;
}

以下是优化前的代码:

public String downPersonQrCode(){
        UserSession us = (UserSession)getSession().getAttribute(SsoConstant.SSO_USER_SESSION_KEY);
        String rootPath = ServletActionContext.getServletContext().getRealPath(
                "/");
        rootPath = rootPath.replaceAll("\\\\", "/");
        //二维码存放路径:
        File path = new File(rootPath + "incoming/glErWeiMa/syzjPerson");
        if (!path.exists()) {
            path.mkdir();
        }
        String qrCodeLink = "http://******.action?credentialcode=" + us.getLoginId() + "&searchType=QrCode";
        BufferedImage img = Encrypt.commonQRCode(qrCodeLink, "png", 7);
        FileOutputStream fos = null;
        File file = null;
        try {
            file = new File(path, us.getLoginId() + ".png");
            if (!file.exists()) {
                file.createNewFile();
            }
            fos = new FileOutputStream(file);
            //此处本可以不用放到文件中,可直接输出!
            ImageIO.write(img, "png", fos);
            fos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        // 下载图片
        HttpServletResponse response = this.getResponse();
        InputStream is = null;
        OutputStream out = null;
        try {
            out = response.getOutputStream();
            byte by[] = new byte[1024];
            response.reset();
            response.setContentType("image/png");
            String outPutPath = rootPath + "incoming/glErWeiMa/syzjPerson/" 
                    + us.getLoginId() + ".png";
            response.addHeader("Content-Disposition",
                    "attachment;filename=\""+us.getLoginId()+".png\"");
            File fileLoad = new File(outPutPath);
            long fileLength = fileLoad.length();
            String length1 = String.valueOf(fileLength);
            response.setHeader("Content_Length", length1);

            if (fileLoad.exists()) {
                byte [] buffer = new byte[0];
                is = new BufferedInputStream(new FileInputStream(outPutPath));
                out = new BufferedOutputStream(response.getOutputStream());
                buffer = new byte[is.available()];
                is.read(buffer);
                out.write(buffer);
                out.flush();    
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(is!=null){
                try {
                    is.close();
                }
                 catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out!=null){
                try {
                    out.close();
                }
                 catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return NONE;
    }

备注:之前的代码是生成图片之后放到文件存储,然后再读取文件再输出,多了一步存在服务器的步骤,后发现没必要就优化了一下。在这里做一个记录,以备忘记后用。
生成二维码的接口不是本人写的,所以就没贴出代码。网上有很多接口可以查到。

代码如有缺陷,欢迎指出!

猜你喜欢

转载自blog.csdn.net/qq_30123829/article/details/82218594