java生成二维码图片,打成压缩包并下载案例

//图片保存路径前缀
private static final String BASE_PATH = "D:/zingImage/";
//压缩包保存路径
private static final String ZIP_PATH = "D:/";
private static final String ZIP_NAME = "zingImage.zip";
//图片保存的格式
private static final String Suffix_PATH = ".png";
//二维码请求路径
private static final String Url_Path = "http://www.baidu.com";


/**
	 * @author 王安其
	 * @desrciption 压缩文件夹并下载
	 * @Date 2019/6/21 14:55 
	 * @Param [params]
	 * @retrun
	 */
@PostMapping(value="/downloadCompressFile")
public void downloadCompressFile(HttpServletResponse response) {
	//生成所有设备的二维码并保存
	saveInspectionImage();
	createCardImgZip(BASE_PATH,ZIP_NAME);
	//下载压缩包
	downLoadZip(ZIP_NAME,ZIP_PATH+ZIP_NAME,response);
}
/**
	 * @author 王安其
	 * @desrciption 保存二维码图片到指定位置
	 * @Date 2019/6/21 14:55 
	 * @Param [params]
	 * @retrun
	 */
	public void saveInspectionImage() {
		try {
			deleteDir(BASE_PATH);
			//查询所有机号
			List> vclInfoList = zingImageService.findVclInfo();
			for(Map map : vclInfoList) {
				String askUrl = Url_Path;
				String savePath = BASE_PATH+map.get("Vcl_No").toString()+Suffix_PATH;
				generateQRCodeImage(askUrl, 350, 350, savePath);
			}
		} catch (Exception e) {
			 e.printStackTrace();
        }
	}
/**
	 * @param text 扫描二维码后访问的路径
	 * @param width 二维码宽度
	 * @param height 二维码高度
	 * @param filePath 图片保存路径
	 * @throws WriterException
	 * @throws IOException
	 */
	private static void generateQRCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException {
		QRCodeWriter qrCodeWriter = new QRCodeWriter();
		BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width,height);
		Path path = FileSystems.getDefault().getPath(filePath);
		MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
	}
public static void deleteDir(String path){
		File file = new File(path);
		String[] content = file.list();//取得当前目录下所有文件和文件夹
		for(String name : content){
			File temp = new File(path, name);
			if(temp.isDirectory()){//判断是否是目录
				deleteDir(temp.getAbsolutePath());//递归调用,删除目录里的内容
				temp.delete();//删除空目录
			}else{
				temp.delete();
			}
		}
	}
/**
	 * @param sourcePath
	 * @param zipName
	 * @return
	 */
	public void createCardImgZip(String sourcePath, String zipName) {  
        String zipPath = ZIP_PATH;  
        File sourceFile = new File(sourcePath);  
        FileInputStream fis = null;  
        BufferedInputStream bis = null;  
        FileOutputStream fos = null;  
        ZipOutputStream zos = null;  
        try {  
            File zipFile = new File(zipPath + "/" + zipName.split("\\.")[0] + ".zip");  
            //如果压缩包存在,删除即可
            if (zipFile.exists()) {  
                 zipFile.delete();
            }   
            fos = new FileOutputStream(zipFile);  
            zos = new ZipOutputStream(new BufferedOutputStream(fos));  
            byte[] bufs = new byte[1024 * 10];  
            // create .zip and put pictures in
            File[] sourceFiles = sourceFile.listFiles();  
            for(int i=0;i
/**
	 * @param fileName 文件名
	 * @param path 文件保存路径(含文件名称)
	 * @param response
	 * @return
	 */
	public String downLoadZip(String fileName,String path,HttpServletResponse response) {  
        try {  
            File file = new File(path);  
            response.setCharacterEncoding("UTF-8");  
            response.setHeader("Content-Disposition",  
                    "attachment; filename=" + new String(fileName.getBytes("UTF-8"), "ISO8859-1"));  
            response.setContentLength((int) file.length()); 
            //定义输出类型  
            response.setContentType("application/zip");
            FileInputStream fis = new FileInputStream(file);  
            BufferedInputStream buff = new BufferedInputStream(fis);
            //相当于我们的缓存
            byte[] b = new byte[1024];  
            //该值用于计算当前实际下载了多少字节  
            long k = 0;
            //从response对象中得到输出流,准备下载  
            OutputStream myout = response.getOutputStream();
            //开始循环下载  
            while (k < file.length()) {  
                int j = buff.read(b, 0, 1024);  
                k += j;  
                myout.write(b, 0, j);  
            }  
            myout.flush();  
            buff.close();  
            file.delete();  
        } catch (Exception e) {  
            System.out.println(e);  
        }  
        return null;  
    }  

 

你可能感兴趣的:(java)