spring java xml 多文件打包下载

@RequestMapping(value = "/index")
	public void index(HttpServletRequest request, HttpServletResponse response, String ids) throws Exception
	{

		Map<String, Object> map = new HashMap<>();
		ArrayList<String> strArray = new ArrayList<String>();
		map.put("id", ids.split(","));
		List<Map<String, Object>> mapList = albumService.getXmlList(map);
		int userId = (int) WebUtils.getSessionAttribute(request, "userId");
		String path = request.getSession().getServletContext().getRealPath("/") + "upload/xml/" + userId + "/";
		deleteFile(new File(path));
		
		Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
		for (int i = 0; i < mapList.size(); i++)
		{
			Element cndXml = doc.createElement("CDN_XML");
			Element askForUpload = doc.createElement("ask_for_upload");
			Element adi = doc.createElement("ADI");
			Element metadata = doc.createElement("Metadata");
			CommonUtil.amsXml(doc, metadata, 1, mapList.get(i));
			Element appData = doc.createElement("App_Data");
			appData.setAttribute("App", "VOD");
			metadata.appendChild(appData);
			appData.setAttribute("Name", "Metadata_Spec_Version");
			metadata.appendChild(appData);
			appData.setAttribute("Value", "CableLabsVOD1.1");
			metadata.appendChild(appData);
			adi.appendChild(metadata);

			Element asset = doc.createElement("Asset");
			Element metadataChild = doc.createElement("Metadata");
			CommonUtil.amsXml(doc, metadataChild, 2, mapList.get(i));
			CommonUtil.appDataXml(doc, metadataChild, 1, mapList.get(i));
			asset.appendChild(metadataChild);

			Element assetChild = doc.createElement("Asset");
			Element metadataChilds = doc.createElement("Metadata");
			CommonUtil.amsXml(doc, metadataChilds, 3, mapList.get(i));
			CommonUtil.appDataXml(doc, metadataChilds, 2, mapList.get(i));
			Element content = doc.createElement("Content");
			content.setAttribute("Value", (String) mapList.get(i).get("ftpAddr"));
			metadataChilds.appendChild(content);
			assetChild.appendChild(metadataChilds);
			asset.appendChild(assetChild);

			adi.appendChild(asset);
			askForUpload.appendChild(adi);
			cndXml.appendChild(askForUpload);

			Transformer transformer = TransformerFactory.newInstance().newTransformer();
			transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
			transformer.setOutputProperty("version", "1.0");
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			DOMSource source = new DOMSource(cndXml);

			File fileDir = new File(path); // 生成xml文件
			if (!fileDir.exists())
			{
				fileDir.mkdirs();
			}
			String filePath = path + mapList.get(i).get("fullspell") + ".xml";
			strArray.add(i, filePath);
			PrintWriter pw = new PrintWriter(new FileOutputStream(filePath));
			StreamResult result = new StreamResult(pw);
			transformer.transform(source, result);
		}
		// 打包zip
		zipDownLoad(request, response, strArray,path);
	}

public void zipDownLoad(HttpServletRequest request, HttpServletResponse response, ArrayList<String> strArray, String path)
			throws IOException
	{
		response.setContentType("APPLICATION/OCTET-STREAM");
		response.setHeader("Content-Disposition", "attachment; filename=" + this.getZipFilename());
		ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
		File[] files = new File[strArray.size()];
		for (int i = 0; i < files.length; i++)
		{
			files[i] = new File(strArray.get(i));
		}
		zipFile(files, "", zos);
		zos.flush();
		zos.close();
	}


	private void zipFile(File[] subs, String baseName, ZipOutputStream zos) throws IOException
	{
		for (int i = 0; i < subs.length; i++)
		{
			File f = subs[i];
			zos.putNextEntry(new ZipEntry(baseName + f.getName()));
			FileInputStream fis = new FileInputStream(f);
			byte[] buffer = new byte[1024];
			int r = 0;
			while ((r = fis.read(buffer)) != -1)
			{
				zos.write(buffer, 0, r);
			}
			fis.close();
		}
	}


	private String getZipFilename()
	{
		Date date = new Date();
		String s = date.getTime() + ".zip";
		return s;
	}


	private void deleteFile(File file)
	{
		if (file.exists())
		{
			if (file.isFile())
			{
				file.delete();
			} else if (file.isDirectory())
			{
				File files[] = file.listFiles();
				for (int i = 0; i < files.length; i++)
				{
					this.deleteFile(files[i]);
				}
			}
			file.delete();
		} else
		{
			System.out.println("所删除的文件不存在!" + '\n');
		}
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值