最近公司搞项目,要求利用Excel文件将数据导入数据库分析,项目组利用jxl对excel文件进行了处理,但出现一个很奇怪的问题:刚开启tomcat时,一切正常,然而当每过一段时间,上传excel文件这里就会有问题,具体症状是:后台不报错,服务正常运行,其他功能依旧正常,但一旦出现错误,就以后上传excel文件均会错误,必须重启Tomcat。。请达人解疑。
系统采用shh web三层架构
以下是利用jxl处理文件的类,
public void save(ClassReport classReport, File fileOnServer, Long classId, String structureId) throws Exception {

// 读Execel内容并写数据到ClassStudentTemp表
try {
System.out.println("批量导入学员信息开始!");
jxl.Workbook wb = Workbook.getWorkbook(fileOnServer);
//得到命名为Data的Sheet
jxl.Sheet st = wb.getSheet("Data");
int colNum = st.getColumns();
int rowNum = st.getRows();
//基本就是在这个地方卡住的
System.out.println("所得行数:"+colNum+"所得列数:"+rowNum);
int n = 0;
for(int i=1; i<rowNum; i++) {
Cell cell1 = st.getCell(0,i);
if(StringUtils.isEmpty(cell1.getContents()))
{
break;
}
n++;
String userId = cell1.getContents();

Cell cell2 = st.getCell(1,i);
String userName = cell2.getContents();

Cell cell3 = st.getCell(2,i);
String sex = cell3.getContents();

//added by wanghuan 2009-10-16
if(!StringUtils.isEmpty(sex))
{
if(sex.indexOf("男") != -1)
{
sex = "0";
}else if(sex.indexOf("女") != -1)
{
sex = "1";
}
}
//end

Cell cell4 = st.getCell(3,i);
String comId = cell4.getContents();
//added by wanghuan 2009-10-12
if(!StringUtils.isEmpty(comId))
{
if(comId.length() != 1 && !comId.trim().equals("G60"))
{
comId = comId.substring(0,1);
}
}
//end

Cell cell5 = st.getCell(4,i);
String staffFlag = cell5.getContents();

//added by wanghuan 2009-9-14
Cell cell6 = st.getCell(5,i);
String mobile = cell6.getContents();
//end

if(StringUtils.isEmpty(userId) || StringUtils.isEmpty(userName) || StringUtils.isEmpty(comId)) continue;

// 内勤时直接插入正式学员表
if(!StringUtils.isEmpty(staffFlag) && (staffFlag.equals("1") || staffFlag.equals("是") )) 

ClassStudent student = new ClassStudent();
student.setClassId(classId);
student.setName(userName);
student.setUserId(userId);
student.setComId(comId);
student.setMobile(mobile);
student.setSex(sex);
classStudentDao.save(student);

UserRoleHelper.createUser(userId, userName, "123", "role007", "from_import_student", comId, "add");
}else{ // 特殊培训中学员帐号是身份证的不用用户校验,直接写入正式学员表
if(structureId.equals("105") || structureId.equals("106"))  

//added by wanghuan 2009-9-10
if(userId.trim().length() == 15 || userId.trim().length() == 18)//学员帐号是身份证
{
IdentificationCodeUtil identificationCodeUtil = new IdentificationCodeUtil ();  
if(identificationCodeUtil.Verify(userId))//是合法身份证
{
ClassStudent student = new ClassStudent();
student.setClassId(classId);
student.setName(userName);
student.setUserId(userId);
student.setComId(comId);
student.setMobile(mobile);
student.setSex(sex);
classStudentDao.save(student);
}else{//非法身份证也插入正式表,但给出提示
ClassStudent student = new ClassStudent();
userId = "身份证号有误! - " + userId;
student.setClassId(classId);
student.setName(userName);
student.setUserId(userId);
student.setComId(comId);
student.setMobile(mobile);
student.setSex(sex);
classStudentDao.save(student);
}
}
//added by wanghuan 2009-10-22
else if(userId.trim().length() != 8)//学员帐号不是9位,是缺少位数的身份证
{
ClassStudent student = new ClassStudent();
userId = "身份证号有误! - " + userId;
student.setClassId(classId);
student.setName(userName);
student.setUserId(userId);
student.setComId(comId);
student.setMobile(mobile);
student.setSex(sex);
classStudentDao.save(student);
}
//end
else
{
// 参加特殊培训的普通外勤
ClassStudentTemp tempStudent = new ClassStudentTemp();
tempStudent.setClassId(classId);
tempStudent.setName(userName);
tempStudent.setUserId(userId);
tempStudent.setComId(comId);
tempStudent.setMobile(mobile);
tempStudent.setSex(sex);
tempStudent.setStaffFlag((StringUtils.isEmpty(staffFlag))?"0":staffFlag); // 默认为0--外勤
//added by wanghuan 2009-10-12
if(tempStudent.getComId().indexOf("G60") != -1)
{
tempStudent.setComId("G");
}
long count = this.ipeAgntinfoManager.countAgntinfo(tempStudent.getUserId(),tempStudent.getComId());
if(count == 0 )
{
tempStudent.setValidFlag("0");//置为验证失败
}else{
tempStudent.setValidFlag("1");
}
//end
classStudentTempDao.save(tempStudent);
}
//end

}else //若是普通培训
{
ClassStudentTemp tempStudent = new ClassStudentTemp();
tempStudent.setClassId(classId);
tempStudent.setName(userName);
tempStudent.setUserId(userId);
tempStudent.setComId(comId);
tempStudent.setMobile(mobile);
tempStudent.setSex(sex);
tempStudent.setStaffFlag((StringUtils.isEmpty(staffFlag))?"0":staffFlag); // 默认为0--外勤
//added by wanghuan 2009-10-12
if(tempStudent.getComId().indexOf("G60") != -1)
{
tempStudent.setComId("G");
}
//added by wanghuan 2009-10-22
long count = 0;
if(userId.trim().length() == 8 )
{
count = this.ipeAgntinfoManager.countAgntinfo(tempStudent.getUserId(),tempStudent.getComId());
}
//end
if(count == 0 )
{
tempStudent.setValidFlag("0");//置为验证失败
}else{
tempStudent.setValidFlag("1");
}
//end
classStudentTempDao.save(tempStudent);
}
}
}
System.out.println("所得行数:"+colNum+"最终列数:"+n);
wb.close();
classReportDao.save(classReport);
}catch(Exception e)
{
System.out.println("批量导入学员出错!!");
e.printStackTrace();
}
} 主要就是在此段代码的前几行,我写注释的地方卡住,下面那条System.out.println("所得行数:"+colNum+"所得列数:"+rowNum);打不出来。