@ Suppresswarnings ("resource" ) Public Void Download () Throws Exception {string filename = "Qrcode.png" ; String content = "Content" ; Bufferedimage Image = Qrcodeutils. encode (content, size ); // Bufferedimage to inputstream Bytearrayoutputstream = New Bytearrayoutputstream (); imageoutputstream imageoutput = ImageIO. createimageoutputstream (bytearrayoutputstream); ImageIO. Write (image, "PNG" , Imageoutput); inputstream = New Bytearrayinputstream (bytearrayoutputstream. tobytearray ()); Long Length = Imageoutput. Length (); // Set 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" )); // Output stream Byte [] Bytes = New Byte [1024 ]; 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 ();}
download the drawing from bufferedimage to inputstream (generate and download the QR code image)