在做附件导出多个excel到压缩包时,导第二个时报错
java.io.IOException: Read error
public Object exportProjectFBListZip(final IEcpDataContext dc, final String typeId, final ItemCondition itemCondition,
final String zipName,final String modelId) {
//根据项目id和状态获取项目具体信息
List<Map> projectList = new ArrayList<Map>();
String fileName = "";
//根据不同的模型调用不同的查询项目信息的接口
if ("2".equals(modelId)) {
fileName = "SCXMFBDC.xls";
projectList = this.getModelResultList(dc, typeId, itemCondition);
}
// 模板文件流
final InputStream templateFileIs = TemplateUtils.getTemplateFileInputStream(fileName);
String fileDir = "";
ZipOutputStream zos = null;
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
final Map<String, InputStream> dataMap = new HashMap<String, InputStream>();
File zipFile = null;
InputStream is = null;
if (projectList.size()>0) {
for(int i=0;i<projectList.size();i++){
String newFileName = projectList.get(i).get("projectno").toString()+projectList.get(i).get("projectname").toString()+".xls";
//根据项目id查询业务部门预测数据
List<Map> ysResultList = this.getSimilarModelysResultList(dc, projectList.get(i).get("projectid").toString());
//根据ywid获取岗位填报和审核人信息
List<ProcessTraceVO> processTraceVOs = new ArrayList<ProcessTraceVO>();
if (projectList.get(i).get("ywid") != null){
processTraceVOs = iWorkFlowTraceQueryContext.findProcessTraceVOByProcessInstID(dc,(Long) projectList.get(i).get("ywid"));
}
//评审参数 也是在单据主表里面取
//根据项目id获取 年度投资预算表数据
List<Map> ndtzList = this.getGeneralQueryRole().getNdtzysList(projectList.get(i).get("projectid").toString());
//TODO 规则结果页签
//创建 Workbook
ItemCondition condition = new ItemCondition();
condition.set("templateFileIs", templateFileIs);
condition.set("fileName", fileName);
condition.set("map", projectList.get(i));
condition.set("ysResultList", ysResultList);
condition.set("processTraceVOs", processTraceVOs);
condition.set("ndtzList", ndtzList);
Workbook exportBook = ModelExportExcelUtil.exportModelFBListInfo(condition);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
exportBook.write(bos);
} catch (IOException e) {
e.printStackTrace();
}
byte[] barray = bos.toByteArray();
is = new ByteArrayInputStream(barray);
dataMap.put(fileDir + "/" + newFileName,is);
}
}
try {
final File dirFile = new File(FileLocator.toFileURL(this.getClass().getResource("/")).toURI());
if (!dirFile.exists()) {
dirFile.mkdirs();
}
zipFile = new File(dirFile, zipName);
if (!zipFile.exists()) {
zipFile.createNewFile();
}
zos = new ZipOutputStream(new FileOutputStream(zipFile));
for (Map.Entry<String, InputStream> entry : dataMap.entrySet()) {
final String key = entry.getKey();
final InputStream valInputStram = entry.getValue();
if (valInputStram != null) {
zos.putNextEntry(new ZipEntry(key));
final BufferedInputStream bi = new BufferedInputStream(valInputStram);
final byte[] buffer = new byte[NumberCommonConst.NUM_1024];
int readCount = bi.read(buffer);
while (readCount != -1) {
zos.write(buffer, 0, readCount);
readCount = bi.read(buffer);
}
// zos.write(bit);
zos.setEncoding("GB2312"); // 不设置此项会导致本地文件夹乱码
// zos.flush();
zos.closeEntry();
}
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {
if (zos != null) {
zos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
byteArrayOut = this.getBytesByFile(zipFile);
processStatusQueryContext.refressProcessStatus(keyWorld, "1");
return byteArrayOut;
}
原因是获取导入模板的Inputstream对象只有一个,第一次导出之后已经将其关闭,下一次获取
final Workbook exportWorkbook = getWorkbook(fileName,fis);
public static Workbook getWorkbook(final String excelFileName,
final InputStream is) {
Workbook workbook = null;
try {
if (excelFileName.endsWith(EXCEL_TYPE_2003)) {
version = version2003;
workbook = WorkbookFactory.create(is);
} else if (excelFileName.endsWith(EXCEL_TYPE_2007)) {
version = version2007;
workbook = new XSSFWorkbook(OPCPackage.open(is));
}
}catch(Exception e){
e.printStackTrace();
}
return workbook;
}
就会报错,
将
// 模板文件流
final InputStream templateFileIs = TemplateUtils.getTemplateFileInputStream(fileName);
放入for循环中就可以解决。