你会用servlet吗??
你先告诉我这个我就告诉你,嘿嘿
http://community.csdn.net/Expert/topic/5723/5723279.xml?temp=.808346

解决方案 »

  1.   

    给你个函数
    /**
     * 压缩文件夹生成后缀名为".zip"的文件并下载
     * 
     * @param folderPath,要压缩的文件夹的路径
     * @param zipFilePath,压缩后文件的路径
     * @param zipFileName,压缩后文件的名称
     * @throws BizException
     */
    public void DownZipFile(String folderPath, String zipFilePath,
    String zipFileName) throws BizException {
    File srcPath = new File(folderPath);
    String outFilename = new String(zipFilePath);
    int len = srcPath.listFiles().length;
    String[] filenames = new String[len]; // 需压缩的文件夹中的文件名
    byte[] buf = new byte[1024];
    try {
    File[] files = srcPath.listFiles();
    for (int i = 0; i < len; i++) {
    // if(!files[i].isDirectory())
    filenames[i] = srcPath.getPath() + File.separator
    + files[i].getName();
    }
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
    outFilename));
    for (int i = 0; i < filenames.length; i++) {
    FileInputStream in = new FileInputStream(filenames[i]);
    out.putNextEntry(new org.apache.tools.zip.ZipEntry(files[i]
    .getName()));
    int bufLen;
    while ((bufLen = in.read(buf)) > 0) {
    out.write(buf, 0, bufLen);
    }
    out.closeEntry();
    in.close();
    }
    out.close();
    } catch (IOException e) {
    log.error(e.getMessage()
    + " occurred in compress fileList operation", e);
    throw new BizException(new SystemMessage("文件夹:" + folderPath
    + " 生成压缩文件 " + zipFileName + " 失败 !"));
    }
    try {
    MyFacesHelper.downLoadFile(new FileInputStream(zipFilePath),
    zipFileName + ".zip", "application/x-download");
    } catch (FileNotFoundException e) {
    log
    .error(e.getMessage()
    + " occurred in down zipfile operation", e);
    throw new BizException(new SystemMessage("文件没有找到:" + zipFileName
    + ",路径:" + zipFilePath));
    } catch (IOException e) {
    log
    .error(e.getMessage()
    + " occurred in down zipfile operation", e);
    throw new BizException(new SystemMessage("输入输出异常!"));
    }
    }
      

  2.   

    给你个函数 
    /** 
      *   压缩文件夹生成后缀名为".zip"的文件并下载 
      *   
      *   @param   folderPath,要压缩的文件夹的路径 
      *   @param   zipFilePath,压缩后文件的路径 
      *   @param   zipFileName,压缩后文件的名称 
      *   @throws   BizException 
      */ 
    public   void   DownZipFile(String   folderPath,   String   zipFilePath, 
    String   zipFileName)   throws   BizException   { 
    File   srcPath   =   new   File(folderPath); 
    String   outFilename   =   new   String(zipFilePath); 
    int   len   =   srcPath.listFiles().length; 
    String[]   filenames   =   new   String[len];   //   需压缩的文件夹中的文件名 
    byte[]   buf   =   new   byte[1024]; 
    try   { 
    File[]   files   =   srcPath.listFiles(); 
    for   (int   i   =   0;   i   <   len;   i++)   { 
    //   if(!files[i].isDirectory()) 
    filenames[i]   =   srcPath.getPath()   +   File.separator 
    +   files[i].getName(); 

    ZipOutputStream   out   =   new   ZipOutputStream(new   FileOutputStream( 
    outFilename)); 
    for   (int   i   =   0;   i   <   filenames.length;   i++)   { 
    FileInputStream   in   =   new   FileInputStream(filenames[i]); 
    out.putNextEntry(new   org.apache.tools.zip.ZipEntry(files[i] 
    .getName())); 
    int   bufLen; 
    while   ((bufLen   =   in.read(buf))   >   0)   { 
    out.write(buf,   0,   bufLen); 

    out.closeEntry(); 
    in.close(); 

    out.close(); 
    }   catch   (IOException   e)   { 
    log.error(e.getMessage() 
    +   "   occurred   in   compress   fileList   operation",   e); 
    throw   new   BizException(new   SystemMessage("文件夹:"   +   folderPath 
    +   "   生成压缩文件   "   +   zipFileName   +   "   失败   !")); 

    try   { 
    MyFacesHelper.downLoadFile(new   FileInputStream(zipFilePath), 
    zipFileName   +   ".zip",   "application/x-download"); 
    }   catch   (FileNotFoundException   e)   { 
    log 
    .error(e.getMessage() 
    +   "   occurred   in   down   zipfile   operation",   e); 
    throw   new   BizException(new   SystemMessage("文件没有找到:"   +   zipFileName 
    +   ",路径:"   +   zipFilePath)); 
    }   catch   (IOException   e)   { 
    log 
    .error(e.getMessage() 
    +   "   occurred   in   down   zipfile   operation",   e); 
    throw   new   BizException(new   SystemMessage("输入输出异常!")); 

    }-------------------------
    很详细了
      

  3.   

    补充一个页面下载连接代码:
    <a href="/..../..../xxx.zip" 
    onMouseOver="this.style.cursor='hand',this.style.color='#FF0000'" 
    onMouseOut="this.style.color='#006699'">点击下载</a>
      

  4.   

    download.jsp
    <%@page contentType="text/html; charset=GBK" language="java" %>
    <%@page import="java.util.*"%>
    <%@page import="java.io.*"%>
    <%@page import="java.net.*"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd";>
    <%
    request.setCharacterEncoding("GBK");
    String fileNameWrite=request.getParameter("file");
    String fileName=new String(request.getParameter("file").getBytes("iso-8859-1"),"GBK");
     String sss=request.getRealPath("\\")+"files\\"+fileName;
      System.out.println(sss);
    if("1".equals(request.getParameter("downloadFlag"))){
       response.setHeader("Content-disposition","attachment; filename="+fileNameWrite);
        }else if("0".equals(request.getParameter("downloadFlag"))){
       response.setContentType("application/ms-word");
    }
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(getServletContext().getRealPath("\\" +"files\\"+ fileName)));
            bos = new BufferedOutputStream(response.getOutputStream());        byte[] buff = new byte[2048];
            int bytesRead;        while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff,0,bytesRead);
            }    } catch(final IOException e) {
            System.out.println ( "IOException." + e );    } finally {
            if (bis != null)
                bis.close();
            if (bos != null)
                bos.close();
        }
    %>
    --------------------------------------------
    index.jsp<%@ page contentType="text/html; charset=GBK" %>
    シシハァウヨ<br><p>
    <a href="download.jsp?file=dow.rar&downloadFlag=1">download<a>
    </p>