apbbbbb
旋转的钢笔
2017-08-07 01:36
采纳率: 19.3%
浏览 1.3k
已采纳

java无法对多个excel打包下载?

java实现多个excel 打包下载,我先创建了一个临时文件夹,然后进行打包下载,最后删除这个临时文件夹, 但是没反应也不报错,求大神指教

 @RequestMapping(value = "downloadPreHomeWorkZIP")
    public void downloadLetterZIP(trainTraineeWorkModel  query, HttpServletResponse response, HttpServletRequest request) throws IOException, SQLException {

        String serverPath =request.getSession().getServletContext().getRealPath("/")+"\\upload\\tempExcel";
         List<File> srcfile=new ArrayList<File>();  
        //在服务器端创建文件夹  
        File file = new File(serverPath);  
        if(!file.exists()){  
            file.mkdir();  
        } 
        List<Map> employees = employeeService     //查询学员上传的课前课后作业
                .getMapTraineesBySessionId(1406940);
        if(employees.size()!=0){
            for (Map map : employees) {
                BLOB blob = (BLOB) map.get("BEFORECLASS_WORK");
                if(blob!=null&&blob.length()!=0){
                String employeeCode=(String)map.get("EMPLOYEE_CODE");

                response.setHeader("Content-Disposition", "attachment; filename=afterSessionWork"+ java.net.URLEncoder.encode(
                        employeeCode + ".xls", "UTF-8"));
                response.setContentType("application/octet-stream; charset=utf-8");
                SimpleDateFormat sfm = new SimpleDateFormat("yyyy-MM-dd"); 
                 String filename = employeeCode + "_" + sfm.format(new Date());  
                    String encodedfileName = new String(filename.getBytes(), "UTF-8"); 
                InputStream in = blob.getBinaryStream();
                 byte[] buf = new byte[1024];
                   int bytesIn = 0;
                   FileOutputStream out = new FileOutputStream(serverPath+ employeeCode+".xls");
                   while ((bytesIn = in.read(buf, 0, 1024)) != -1) {
                    out.write(buf, 0, bytesIn);
                   }
                   srcfile.add(new File(serverPath+"\\"+encodedfileName+".xls"));  
                   in.close();
                   out.close();
                }

            }
            //将服务器上存放Excel的文件夹打成zip包  
            File zipfile = new File(serverPath+"PreWork"+".zip");  
            ZipUtils.zipFiles(srcfile, zipfile);  
//          //下载  
            ZipUtils.downFile(response,serverPath, "PreWork"+".zip");  
//          return null;  
        }

        //工具类

        public class ZipUtils {
    /** 
     * 将多个Excel打包成zip文件 
     * @param srcfile 
     * @param zipfile 
     */  
    public static void zipFiles(List<File> srcfile, 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.size(); i++) {    
                File file = srcfile.get(i);    
                FileInputStream in = new FileInputStream(file);    
                // Add ZIP entry to output stream.    
                out.putNextEntry(new ZipEntry(file.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();  
        }    
    }    


    public static void downFile(HttpServletResponse response,String serverPath, String str) {    
        try {    
            String path = serverPath + str;    
            File file = new File(path);    
            if (file.exists()) {    
                InputStream ins = new FileInputStream(path);    
                BufferedInputStream bins = new BufferedInputStream(ins);// 放到缓冲流里面    
                OutputStream outs = response.getOutputStream();// 获取文件输出IO流    
                BufferedOutputStream bouts = new BufferedOutputStream(outs);    
                response.setContentType("application/x-download");// 设置response内容的类型    
                response.setHeader(    
                        "Content-disposition",    
                        "attachment;filename="    
                                + URLEncoder.encode(str, "UTF-8"));// 设置头部信息    
                int bytesRead = 0;    
                byte[] buffer = new byte[8192];    
                 //开始向网络传输文件流    
                while ((bytesRead = bins.read(buffer, 0, 8192)) != -1) {    
                   bouts.write(buffer, 0, bytesRead);    
               }    
               bouts.flush();// 这里一定要调用flush()方法    
                ins.close();    
                bins.close();    
                outs.close();    
                bouts.close();    
            } else {    
                response.sendRedirect("../error.jsp");    
            }    
        } catch (IOException e) {    
            e.printStackTrace();  
        }    
    }  
}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • apbbbbb
    旋转的钢笔 2017-08-11 06:35
    已采纳

    最后问题解决了,我路径写的有问题,还有就是不能使用ajax提交,不然无法下载

    点赞 评论
  • qq_35728177
    Tsui丶 2017-08-07 02:07
    点赞 评论

相关推荐