压缩文件

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

 

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包包含有该两类

你可能感兴趣的:(apache,sql,servlet,ant)