博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java文件下载(生成多个二维码文件,下载zip文件)
阅读量:4230 次
发布时间:2019-05-26

本文共 2935 字,大约阅读时间需要 9 分钟。

最近公司有个需求,需要点击一个按钮生成二维码,然后把二维码打包成zip文件,然后下载zip文件,于是我做了个demo,这里分享一下。

文件下载不能异步请求,因为我需要url后面动态拼接参数,所以这里用了button,具体前台代码如下可以实现下载(看压缩文件下载就可以了):

下载

后台主要分为这几个步骤:1生成二维码,2生成zip文件,3zip文件输出到输出流,4删除生成的文件。具体代码如下:

package com.mr.smart;import com.google.zxing.WriterException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.*;import java.net.URLEncoder;import java.util.Arrays;import java.util.List;@WebServlet(name = "ZipDownLoadServlet", value = "/zipDownLoadServlet")public class ZipDownLoadServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String realPath = request.getSession().getServletContext().getRealPath("/"); //1 生成二维码 String projectName = "农银公杂费"; String path = realPath + projectName; File file = new File(path); if (!file.exists()) { file.mkdirs(); } List
list = Arrays.asList("code11.jpg", "code22.jpg", "code33.jpg");//已经存好的图片 try { for (String x : list) { String source = "G://" + x; String qrCode = path + "/公杂费_" + x; String url = "https://www.baidu.com/?name=" + x; QRCodeUtil.generateQRImage(url, qrCode, source);//生成二维码的方法 } } catch (WriterException e) { e.printStackTrace(); } //2 生成zip文件 ZipHelper.zipCompress(path, path + ".zip"); //3 下载 String zipFileName = path + ".zip"; String filename = projectName + ".zip"; //设置文件MIME类型 response.setContentType(getServletContext().getMimeType(filename)); response.setCharacterEncoding("UTF-8"); //设置Content-Disposition response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "utf-8")); InputStream in = new FileInputStream(zipFileName); OutputStream out = response.getOutputStream(); //写文件 int b; while ((b = in.read()) != -1) { out.write(b); } out.flush(); //4 删除多余文件 deleteDir(new File(path)); in.close();//先关闭输入流才能删除 deleteDir(new File(zipFileName)); out.close(); } private boolean deleteDir(File dir) { if (dir.isDirectory()) { String[] children = dir.list(); for (String s : children) { boolean success = deleteDir(new File(dir, s)); if (!success) { return false; } } } // 目录此时为空,可以删除 return dir.delete(); }}

中间用到了生成二维码的工具类,生成zip文件的工具类,具体代码见:

转载地址:http://eqjqi.baihongyu.com/

你可能感兴趣的文章
C#.net Web Developer's Guide
查看>>
Virtualization with VMware ESX Server
查看>>
System Architecture with XML
查看>>
Microsoft SQL Server 2005 Stored Procedure Programming in T-SQL & .NET
查看>>
Sams Teach Yourself Microsoft Office Access 2003 in 24 Hours
查看>>
Leveraging Web Services: Planning, Building, and Integration for Maximum Impact
查看>>
Implementing Backup and Recovery: The Readiness Guide for the Enterprise
查看>>
Wireless Communications over MIMO Channels: Applications to CDMA and Multiple Antenna Systems
查看>>
UMTS Performance Measurement: A Practical Guide to KPIs for the UTRAN Environment
查看>>
Grid Networks: Enabling Grids with Advanced Communication Technology
查看>>
Communication Systems for the Mobile Information Society
查看>>
Beginning Ubuntu Linux: From Novice to Professional
查看>>
IPsec Virtual Private Network Fundamentals
查看>>
Algorithms and Networking for Computer Games
查看>>
Java Regular Expressions: Taming the java.util.regex Engine
查看>>
CSS Instant Results
查看>>
The Vest Pocket Guide to Information Technology
查看>>
Beginning MySQL
查看>>
Uncertainty and Information: Foundations of Generalized Information Theory
查看>>
Professional Web APIs with PHP: eBay, Google, Paypal, Amazon, FedEx plus Web Feeds
查看>>