日期:2014-05-18  浏览次数:20786 次

一次下载多个文件
public static void download(HttpServletRequest request,  
            HttpServletResponse response, String storeName, String contentType,  
            String realName) throws Exception {  
        response.setContentType("text/html;charset=UTF-8");  
        request.setCharacterEncoding("UTF-8");  
        BufferedInputStream bis = null;  
        BufferedOutputStream bos = null;  
        String ctxPath = System.getProperty("user.dir")  +"/" 
                + FileOperateUtil.UPLOADDIR;  
        String downLoadPath = ctxPath + storeName;  
        long fileLength = new File(downLoadPath).length();  
        response.setContentType(contentType);  
        response.setHeader("Content-disposition", "attachment; filename="  
                + new String(realName.getBytes("utf-8"), "ISO8859-1"));  
        response.setHeader("Content-Length", String.valueOf(fileLength));  
        bis = new BufferedInputStream(new FileInputStream(downLoadPath));  
        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);  
            System.out.println("sss");
        }  
        bis.close();  
        bos.close();  
    }

写了一个下载方法,我把这个方法循环2次,为什么只下载了一次。是request,response的缘故吗
------解决方案--------------------
我感觉你没有理解http协议,http一问一答的形式,你问一次,就取一个文件,response.setHeader("Content-Length", String.valueOf(fileLength)); 也设置死了的,你一定要返回两个文件是闹哪样?