zoukankan      html  css  js  c++  java
  • 从BufferedImage到InputStream,实现绘图后进行下载(生成二维码图片并下载)

    @SuppressWarnings("resource")
    public void download() throws Exception{
        String filename = "qrcode.png";
        String content = "content";
        BufferedImage image = QRcodeUtils.encode(content, size);
        
        //BufferedImage 转 InputStream
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ImageOutputStream imageOutput = ImageIO.createImageOutputStream(byteArrayOutputStream);
        ImageIO.write(image, "png", imageOutput);
        InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        long length = imageOutput.length();
        
        //设置response
        HttpServletResponse response = this.getResponse();
        response.setContentType("application/x-msdownload");
        response.setContentLength((int)length);
        response.setHeader("Content-Disposition","attachment;filename="+new String(filename.getBytes("gbk"),"iso-8859-1"));
        
        //输出流
        byte[] bytes = new byte[1024];
        OutputStream outputStream = response.getOutputStream();
        long count = 0;
        while(count < length){
            int len = inputStream.read(bytes, 0, 1024);
            count +=len;
            outputStream.write(bytes, 0, len);
        }
        outputStream.flush();
    }
  • 相关阅读:
    聊聊Docker数据卷和数据卷容器
    Nginx虚拟主机
    Shell文本处理四剑客
    Docker镜像、容器剖析
    Tomcat性能优化及JVM内存工作原理
    Nginx动静分离架构&&HA-LB集群整合
    Mysql主从复制架构实战
    Mysql编译安装详解
    Apache虚拟主机实战
    Ansible性能调优
  • 原文地址:https://www.cnblogs.com/rubekid/p/3931044.html
Copyright © 2011-2022 走看看