博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java文件下载(生成多个二维码文件,下载zip文件)
阅读量:4231 次
发布时间: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 语言静态队列的简单实现
查看>>
[linux] unix domain socket 例子
查看>>
[linux] c 实现简单的web服务器
查看>>
栈--判断回文字符串
查看>>
Dijkstra算法--一个点到其余点最短路径
查看>>
解决 The `master` repo requires CocoaPods 1.0.0 - (currently using 0.39.0)
查看>>
gdb调试常用命令
查看>>
vim正则表达式批量修改文本
查看>>
objective-c init方法的写法
查看>>
极客公园 Mac 第三方客户端(swift)
查看>>
【Lintcode】寻找峰值
查看>>
Arduino 串口读写 SD 卡模块
查看>>
图的基本算法--深度优先搜索(dfs) 和 广度优先搜索(bfs)
查看>>
[Linux] Linux内核编译安装过程,及Linux源码目录结构
查看>>
[Linux] c语言变量的存储位置-笔记
查看>>
[Linux] 头文件实质-笔记
查看>>
统一修改iOS中xib颜色值
查看>>
数据湖与数据仓库的新未来:阿里提出湖仓一体架构
查看>>
基于 Flink+Iceberg 构建企业级实时数据湖 | 附 PPT 下载
查看>>
Flink 源码:Checkpoint 元数据详解
查看>>