3. download.jsp:
<%
response.reset();
response.setContentType("application/msexcel");
OutputStream os = response.getOutputStream();
response.setHeader("Content-disposition","attachment; filename=courseware.xls");
byte[] bytes = ...
os.write(bytes);
os.close();
%>

解决方案 »

  1.   

    response.setContentType("application/msdownload");
      

  2.   

    download is okay now
    now only left :
    ow to easily zip a directory
      

  3.   

    3.import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipOutputStream;public class FileZip
    {
    static final char SEPARATOR = File.separatorChar;
    private int preffixLen = 0; public void compress(OutputStream out, File file) throws IOException
    {
    preffixLen = file.getParentFile().getAbsolutePath().length() + 1;
    ZipOutputStream zos = new ZipOutputStream(out);
    addFile(zos, file);
    zos.close();
    } private void addFile(ZipOutputStream zos, File file) throws IOException
    {
    String entryName = entryName(file);
    if (entryName.equals("") || entryName.equals("."))
    return; System.out.println("adding " + entryName);
    ZipEntry entry = new ZipEntry(entryName);
    entry.setTime(file.lastModified());
    boolean isDirectory = file.isDirectory();
    if (isDirectory)
    {
    entry.setMethod(0);
    entry.setSize(0L);
    entry.setCrc(0L);
    }
    else
    entry.setSize(file.length()); zos.putNextEntry(entry);
    if (isDirectory)
    {
    File[] files = file.listFiles();
    for(int i=0; i<files.length; i++)
    addFile(zos, files[i]);
    }
    else
    {
    byte buf[] = new byte[1024];
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
    int bytes;
    while ((bytes = bis.read(buf, 0, buf.length)) != -1)
    zos.write(buf, 0, bytes);
    bis.close();
    }
    zos.closeEntry();
    } private String entryName(File file)
    {
    String entryName = file.getPath();
    if (file.isDirectory())
    entryName = entryName.endsWith(File.separator) ? entryName : entryName + File.separator;
    entryName = entryName.replace(File.separatorChar, '/').substring(preffixLen);
    return entryName;
    } public static void main(String args[])
    {
    try
    {
    FileOutputStream fos = new FileOutputStream("test.zip");
    FileZip zip = new FileZip();
    zip.compress(new BufferedOutputStream(fos), new File("d:\\test\\jsp")); //compress folder d:\test\jsp
    fos.close();
    }
    catch (Throwable e)
    {
    e.printStackTrace();
    }
    }
    }
      

  4.   

    正确。我再来加一段从JSP download 一个目录中的所有文件的代码。已经过测试<%!
     public void doZipDirectory(File d, ZipOutputStream out,String rootDir)throws IOException, IllegalArgumentException {
         // Check that the directory is a directory, and get its contents
         if (!d.isDirectory())throw new IllegalArgumentException("Compress: not a directory: " +d);
         String[] entries = d.list();
         byte[] buffer = new byte[4096];  // Create a buffer for copying
         int bytes_read;      // Loop through all entries in the directory
         for(int i = 0; i < entries.length; i++) {
             File f = new File(d, entries[i]);
             //System.out.println(entries[i]);
             if (f.isDirectory())  {
                 doZipDirectory(f, out,rootDir);
             }
             else
             {
                  FileInputStream in = new FileInputStream(f); // Stream to read file
                  String path=f.getPath();
                  String relevantPath=path.substring(path.indexOf(rootDir)+rootDir.length());
                  ZipEntry entry = new ZipEntry(relevantPath);  // Make a ZipEntry
                  out.putNextEntry(entry);                     // Store entry
                  while((bytes_read = in.read(buffer)) != -1)  // Copy bytes
                      out.write(buffer, 0, bytes_read);
                  in.close();                                  // Close input stream
             }
          }
     }%>
    <%
    try{
            //getParameter
         String courseware_ID=PatchUtil.nvl(request.getParameter("courseware_ID"));
         String coursewareName=getCoursewareName(courseware_ID); //find dir
            java.io.File file=new java.io.File("");
            String separator=java.io.File.separator;
            String rootDir=file.getAbsolutePath()+separator+"config"+separator+"nebo"+
              separator+"applications"+separator+"NEBOWEBAPP"+separator+"resource"+separator;
            String strCourseDir=rootDir+"Course_"+coursewareName+separator;
            File courseDir=new java.io.File(strCourseDir);
         if(courseDir.exists() && courseDir.isDirectory());
         else
           courseDir.mkdirs();       //generate zip file
         //download
            response.reset();
         response.setContentType("application/msdownload");
         OutputStream os = response.getOutputStream();
         response.setHeader("Content-disposition","attachment; filename=courseware.zip");        ZipOutputStream zos=new ZipOutputStream(os);
            doZipDirectory(new File(strCourseDir),zos,rootDir);     zos.close();
    }
    catch(Exception e){
    e.printStackTrace();
            //throw new Exception(e);
    }
    %>