旋转的钢笔 2017-08-07 01:36 采纳率: 54.5%
浏览 1296
已采纳

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条回答

  • 旋转的钢笔 2017-08-11 06:35
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化