How to generate QR Code Compression and Export function in batch by springboot+hutool
This article mainly introduces how springboot+hutool batch generates QR code compression export function, the article introduces in great detail, has a certain reference value, interested friends must read it!
1. Introduce dependency com.google.zxing core 3.4.1 cn.hutool hutool-all 5.7.14 2. Test code QrConfig config = new QrConfig (300,300); / / set the margin, the margin between the QR code and the background config.setMargin (3); / / set the foreground color, the QR code color (cyan) config.setForeColor (Color.CYAN); / / set the background color (gray) config.setBackColor (Color.GRAY) / / generate a QR code to a file, or stream QrCodeUtil.generate ("12345678", config, FileUtil.file ("E:/image/12345678.jpg"))
3. Batch generation
However, if we want to generate a batch, it is impossible to rely entirely on manual input, and then we implement the batch generation of imported excel tables.
You need to introduce poi dependencies:
4. Parsing excel org.apache.poi poi-ooxml 5.0.0
Prepare a form:
Test read:
5. Batch image compression
It is not convenient to output or download a single picture, so we need to compress it.
Batch export of compressed files
The code is as follows
/ * package the file into zip and download * / @ PostMapping (value = "xiazai", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) @ ApiOperation ("Export package") public void download (HttpServletResponse response) throws IOException {response.setHeader ("content-type", "application/octet-stream"); response.setHeader ("Access-Control-Expose-Headers", "Content-Disposition") Response.setContentType ("application/octet-stream"); response.setHeader (HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=download.zip"); CompressUtil.createArchiver (CharsetUtil.CHARSET_UTF_8, ArchiveStreamFactory.ZIP,response.getOutputStream ()) .add (FileUtil.file ("E:/image")); / / ZipUtils.pngZip (response.getOutputStream (), qrImages);} 6. Upload excel to directly convert the output stream into a compressed package
Try to upload excel to generate a QR code package and download it (no files will be generated on the server)
The code is as follows:
/ * Image content and name * / @ Datapublic class QrImage {private byte [] bytes; private String name;} / * Export the generated QR code byte stream to * @ param outputStream * @ param qrImages * / public static void pngZip (OutputStream outputStream,List qrImages) {/ / Zip output stream ZipOutputStream zipOutputStream = null; try {zipOutputStream = new ZipOutputStream (outputStream) For (QrImage file: qrImages) {ZipEntry zipEntry = new ZipEntry (file.getName () + ".png"); zipOutputStream.putNextEntry (zipEntry); / / write data zipOutputStream.write (file.getBytes (), 0, file.getBytes () .length); zipOutputStream.flush ();} zipOutputStream.flush () ZipOutputStream.close ();} catch (IOException e) {e.printStackTrace ();} finally {/ / close the stream try {if (zipOutputStream! = null) {zipOutputStream.close () } if (outputStream! = null) {outputStream.close ();}} catch (IOException e) {e.printStackTrace () Package the file into zip and download * / @ PostMapping (value = "xiazai", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) @ ApiImplicitParam (name = "excelFile", value = "excel Import", required = true,dataType= "MultipartFile", allowMultiple = true,paramType = "query") @ ApiOperation ("Export package") public void download (@ RequestParam ("excelFile") MultipartFile file HttpServletResponse response) throws IOException {/ / read excel ExcelReader reader = ExcelUtil.getReader (file.getInputStream ()) List lists = reader.read (); / / Delete the title lists.remove (0); / / batch generate the QR code List qrImages = create (lists); response.setHeader ("content-type", "application/octet-stream"); response.setHeader ("Access-Control-Expose-Headers", "Content-Disposition"); response.setContentType ("application/octet-stream") Response.setHeader (HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=download.zip"); / / CompressUtil.createArchiver (CharsetUtil.CHARSET_UTF_8, ArchiveStreamFactory.ZIP,response.getOutputStream ()) / / .add (FileUtil.file ("E:/image")); ZipUtils.pngZip (response.getOutputStream (), qrImages);} public List create (List list) throws FileNotFoundException {QrConfig config = new QrConfig (300,300) / / set the margin between the QR code and the background config.setMargin (3); / / set the foreground color, both the QR code color (cyan) config.setForeColor (Color.CYAN); / / set the background color (gray) config.setBackColor (Color.GRAY); byte [] bytes=null; List qrImages = new ArrayList (); QrImage qrImage / / generate the QR code to the file, or stream for (List objects: list) {/ / use the first column as the QR code content qrImage = new QrImage (); / / use the first column as the QR code content bytes = QrCodeUtil.generatePng (objects.get (0) .toString (), config.setImg ("E:/image/logo.png")) QrImage.setBytes (bytes); qrImage.setName (objects.get (0). ToString ()); qrImages.add (qrImage);} return qrImages;} are all the contents of this article entitled "how to batch generate QR Code Compression and Export function by springboot+hutool". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!