开发者社区> 问答> 正文

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

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

 @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();  
        }    
    }  
}

 

展开
收起
爱吃鱼的程序员 2020-06-08 11:52:03 619 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    打断点在downFile方法,看看有没有数据发送到客户端

    2020-06-08 11:52:18
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载