首先上传zip文件到数据库,部分代码如下: ReportTemplateAddActionForm rtaf = (ReportTemplateAddActionForm) form; FormFile file = rtaf.getTemplateFileName(); String filename = file.getFileName(); rt.setTemplateFileName(filename); byte[] b = null; try{ InputStream in = file.getInputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); b = new byte[1024 * 10];//得到2进制文件 int i = in.read(b); while(i != -1) { bos.write(b, 0, i); i = in.read(b); } b =bos.toByteArray(); in.close(); bos.close(); 下来从数据库下载zip文件并解压到指定目录下 ServletContext servletContext = request.getSession().getServletContext(); String[] filename = rt.getTemplateFileName().split("//."); String filepath = servletContext.getRealPath("/resources/reporttemplate")+ "//"; iReportDecompressionUtil.writeFile(rt, filepath);//写文件 iReportDecompressionUtil.decompress(filepath + rt.getTemplateFileName());//解压文件 //删除压缩文件 File file = new File(filepath + rt.getTemplateFileName()); if(file.exists()){ file.delete(); } package com.aisino.grain.util; import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.zip.CRC32; import java.util.zip.CheckedInputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import com.aisino.grain.dao.pojo.ReportTemplate; /** * 报表工具类 * * @author jwd */ public class iReportDecompressionUtil { public static final String EXT = ".zip"; private static final String BASE_DIR = ""; private static final String PATH = File.separator; private static final int BUFFER = 1024; /** * 文件 解压缩 * * @param srcPath 源文件路径 * @throws Exception */ public static void decompress(String srcPath) throws Exception { File srcFile = new File(srcPath); decompress(srcFile); } /** * 解压缩 * * @param srcFile * @throws Exception */ public static void decompress(File srcFile) throws Exception { String basePath = srcFile.getParent(); decompress(srcFile, basePath); } /** * 解压缩 3 * * @param srcFile * @param destFile * @throws Exception */ public static void decompress(File srcFile, File destFile) throws Exception { CheckedInputStream cis = new CheckedInputStream(new FileInputStream( srcFile), new CRC32()); ZipInputStream zis = new ZipInputStream(cis); decompress(destFile, zis); zis.close(); } /** * 解压缩 2 * * @param srcFile * @param destPath * @throws Exception */ public static void decompress(File srcFile, String destPath) throws Exception { decompress(srcFile, new File(destPath)); } /** * 文件 解压缩 1 * * @param srcPath 源文件路径 * @param destPath 目标文件路径 * @throws Exception */ public static void decompress(String srcPath, String destPath) throws Exception { File srcFile = new File(srcPath); decompress(srcFile, destPath); } /** * 文件 解压缩 4 * * @param destFile 目标文件 * @param zis ZipInputStream * @throws Exception */ private static void decompress(File destFile, ZipInputStream zis) throws Exception { ZipEntry entry = null; while ((entry = zis.getNextEntry()) != null) { // 文件 String dir = destFile.getPath() + File.separator + entry.getName(); File dirFile = new File(dir); // 文件检查 fileProber(dirFile); if (entry.isDirectory()) { dirFile.mkdirs(); } else { decompressFile(dirFile, zis); } zis.closeEntry(); } } /** * 文件探针当父目录不存在时,创建目录! 5 * * @param dirFile */ private static void fileProber(File dirFile) { File parentFile = dirFile.getParentFile(); if (!parentFile.exists()) { // 递归寻找上级目录 fileProber(parentFile); parentFile.mkdir(); } } /** * 文件解压缩 6 * * @param destFile 目标文件 * @param zis ZipInputStream * @throws Exception */ private static void decompressFile(File destFile, ZipInputStream zis) throws Exception { BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(destFile)); int count; byte data[] = new byte[BUFFER]; while ((count = zis.read(data, 0, BUFFER)) != -1) { bos.write(data, 0, count); } bos.close(); } public static void writeFile(ReportTemplate rt, String filepath) { InputStream inputStream = null; if (null != rt) { String fileName = rt.getTemplateFileName(); inputStream = new ByteArrayInputStream(rt.getTemplateFile()); try { File file = new File(filepath + fileName); FileOutputStream fos = new FileOutputStream(file); byte[] b = new byte[1024*10]; int len = 0; //read(b)从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中 while ((len = inputStream.read(b))!= -1){ //ops.write(b, 0, len); fos.write(b, 0, len); } inputStream.close(); fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }