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();
}
}
}
打断点在downFile方法,看看有没有数据发送到客户端
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。