压缩文件

   最近要实现根据用户选择的文件,将文件从数据库取出,并打包下载,简单实现代码如下:

 

PreparedStatement ps = null;
		ResultSet rs = null;
		ZipOutputStream zos = null;
		File file = new File(path);
		if (!(file.exists() && file.isDirectory())) {
			file.mkdirs();
		}
		try {
			ps = conn.prepareStatement(sql.toString());
			ps.setString(1, ids[0]);
			for (int i = 0; i < ids.length - 1; i++) {
				ps.setString(i + 2, ids[i + 1]);
			}
			rs = ps.executeQuery();
			Blob blob = null;
			String filename = null;
			InputStream input = null;
			zos = new ZipOutputStream(new FileOutputStream(path + "\\"
					+ tempname));
			while (rs.next()) {
				try {
					filename = rs.getString("name");
					blob = rs.getBlob("content");
					zos.putNextEntry(new ZipEntry(filename));
					zos.setEncoding("GBK");
					if (blob != null) {
						input = blob.getBinaryStream();
						int len;
						while ((len = input.read()) != -1) {
							zos.write(len);
						}
					}
				} catch (IOException e) {
					e.printStackTrace();
				} finally {
					try {
						zos.closeEntry();
					} catch (IOException e) {
						e.printStackTrace();
					}
					try {
						if (input != null)
							input.close();
					} catch (IOException e) {
						input = null;
						e.printStackTrace();
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (zos != null)
					zos.close();
			} catch (IOException e) {
				zos = null;
				e.printStackTrace();
			}
			if (rs != null)
				try {
					rs.close();
				} catch (SQLException e) {
				}
			if (ps != null)
				try {
					ps.close();
				} catch (SQLException e) {
				}
			if (conn != null)
				try {
					conn.close();
				} catch (SQLException e) {
				}
		}

 调用的servlet代码如下:

 

@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		String path = Application.propertiesMap.get("download.temp.path");
		String stuName = req.getSession().getAttribute("name").toString();
		String filename = stuName + "xxx.zip";
		String tempname = System.currentTimeMillis()+"";
		OutputStream output = resp.getOutputStream();
		String[] ids = req.getParameterValues("document_id");
		Download tes = new Download();
		try {
			tes.tes(ids, path,tempname);
		} catch (Exception e) {
			e.printStackTrace();
		}
		File file = new File(path+"\\"+tempname);
		InputStream input =null;
		try {
			resp.setContentLength((int) file.length());
			resp.setHeader("Content-Disposition", "attach;filename="
					+ new String(filename.getBytes("gb2312"), "ISO8859-1"));
			resp.setContentType("application/octet-stream;charset=utf-8");
			input = new FileInputStream(file);
			byte[] bt = new byte[1024];
			while ((input.read(bt)) != -1) {
				output.write(bt);
			}
		} catch (Exception e) {
			resp.setContentType("text/html;charset=UTF-8");
			PrintWriter out = resp.getWriter();
			out.println("<script>alert('该文件不存在或者已被删除');window.close();</script>");
			out.flush();
			out.close();
		} finally {
			input.close();
			output.flush();
			output.close();
			boolean bool = file.delete();
			System.out.println("删除临时文件");
			if (bool == false)
				System.out.println("删除文件失败");
		}
	}

 会使用到apache的

 

org.apache.tools.zip.ZipEntry;

org.apache.tools.zip.ZipOutputStream;

可以使用ant包包含有该两类

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值