首页 > 代码库 > 从BufferedImage到InputStream,实现绘图后进行下载(生成二维码图片并下载)
从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();}
从BufferedImage到InputStream,实现绘图后进行下载(生成二维码图片并下载)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。