日期:2014-05-20 浏览次数:20766 次
LZ想法很好。我来帮你实现: 首先通过压缩方式: /*************************************************************************** * 压缩文件 * * @author Louis * @param srcfile * 需要压缩的文件列表 * @param zipfile * 压缩后的文件 * @author Louis */ public static void ZipFiles(java.io.File[] srcfile, java.io.File zipfile) { byte[] buf = new byte[1024]; try { // Create the ZIP file ZipOutputStream out = new ZipOutputStream(new FileOutputStream( zipfile)); // Compress the files for (int i = 0; i < srcfile.length; i++) { FileInputStream in = new FileInputStream(srcfile[i]); // Add ZIP entry to output stream. out.putNextEntry(new ZipEntry(srcfile[i].getName())); // Transfer bytes from the file to the ZIP file int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } // Complete the entry out.closeEntry(); in.close(); } // Complete the ZIP file out.close(); } catch (IOException e) { e.printStackTrace(); } } /*************************************************************************** * 解压缩文件 * * @author Louis * @param zipfile * 需要解压缩的文件 * @param descDir * 解压后的文件目录 * @author Louis */ public static void UnZipFiles(java.io.File zipfile, String descDir) { try { // Open the ZIP file ZipFile zf = new ZipFile(zipfile); for (Enumeration entries = zf.entries(); entries.hasMoreElements();) { // Get the entry name ZipEntry entry = ((ZipEntry) entries.nextElement()); String zipEntryName = entry.getName(); InputStream in = zf.getInputStream(entry); // System.out.println(zipEntryName); OutputStream out = new FileOutputStream(descDir + zipEntryName); byte[] buf1 = new byte[1024]; int len; while ((len = in.read(buf1)) > 0) { out.write(buf1, 0, len); } // Close the file and stream in.close(); out.close(); } } catch (IOException e) { e.printStackTrace(); } } /** * 下载服务器中的txt文件 * @param filePath * 需要下载的文件File包装类 * @param filename * 下载后默认的文件名 * @param response * * @throws Exception */ public static void download(File filePath, String filename, HttpServletResponse response) throws Exception{ // TODO Auto-generated method stub BufferedInputStream bis = null; BufferedOutputStream bos = null; OutputStream fos = null; InputStream fis = null; //File uploadFile = new File(filePath); File uploadFile = filePath; fis = new FileInputStream(uploadFile); bis = new BufferedInputStream(fis); fos = response.getOutputStream(); bos = new BufferedOutputStream(fos); filename = URLEncoder.encode(filename, "GBK"); //弹出下载对话框的关键代码 response.setContentType("application/x-download"); response.setHeader("Content-Disposition","attachment;filename="+ filename); int bytesRead = 0; //都是用输入流进行先读,然后用输出流去写,用的是缓冲输入输出流 byte[] buffer = new byte[8192]; while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) { try{ bos.write(buffer, 0, bytesRead); }catch(Exception e){} } try{bos.flush();}catch(Exception e){} try{bos.close();}catch(Exception e){} try{bis.close();}catch(Exception e){} try{fos.close();}catch(Exception e){} try{fis.close();}catch(Exception e){} } /** * 上传文件并返回上传后的文件名 * * @param uploadFileName * 被上传的文件名称 * @param savePath * 文件的保存路径 * @param uploadFile * 被上传的文件 * @return 成功与否 * @throws IOException */ public static String uploadForName(String uploadFileName, String savePath, File uploadFile) throws IOException { String newFileName = checkFileName(uploadFileName, savePath); FileOutputStream fos = null; FileInputStream fis = null; try { fos = new FileOutputStream(savePath +"/"+ newFileName); fis = new FileInputStream(uploadFile); byte[] buffer = new byte[1024]; int len = 0; while ((len = fis.read(buffer)) > 0) { fos.write(buffer, 0, len); } } catch (FileNotFoundException e) { throw e; } catch (IOException e) { throw e; } finally { try { if (fos != null) { fos.close(); } if (fis != null) { fis.close(); } } catch (IOException e) { throw e; } } return newFileName; } ****************************************************************************** 上面的类只需要LZ根据逻辑组合一下。就能实现文件夹压缩或者解压缩上传下载了。。。 关于下载文件夹的问题。。我能想到的就只能是通过 写一个bat文件。然后通过java程序来调用实现。。