POI读取EXCEL导入数据库后,此EXCEL无法删除文件,请看代码?在线等……
POI读取EXCEL导入数据库CLASS代码:
package poi;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class POITest {
public static Connection conn = null;
public static Statement stmt = null;
public static boolean connectDB2() {
try {
Class.forName( "org.gjt.mm.mysql.Driver ");
String url = "jdbc:mysql://localhost:3306/dstore ";
conn = DriverManager.getConnection(url, "root ", "111111 ");
stmt = conn.createStatement();
}
//捕获加载驱动程序异常
catch (ClassNotFoundException cnfex) {
System.err.println( "装载JDBC驱动程序失败。 ");
cnfex.printStackTrace();
return false;
}
//捕获连接数据库异常
catch (SQLException sqlex) {
System.err.println( "无法连接数据库 ");
sqlex.printStackTrace();
//System.exit(1); // terminate program
return false;
}
return true;
}
public static boolean readExcelToDB2(String userName) {
POIFSFileSystem fs = null;
HSSFWorkbook wb = null;
try {
fs = new POIFSFileSystem(new FileInputStream( "E:\\Tomcat 5.0\\webapps\\sms_cnc_rs\\upload\\ "+userName+ ".xls "));
wb = new HSSFWorkbook(fs);
} catch (IOException e) {
e.printStackTrace();
return false;
}
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = null;
HSSFCell cell = null;
String name = " ";
long aa =0;
String company = " ";
long phone =0;
long mobile =0;
String email = " ";
String zu = " ";
int rowNum, cellNum;
int i;
rowNum = sheet.getLastRowNum();
for (i = 0; i <= rowNum; i++) {
row = sheet.getRow(i);
//cellNum = row.getLastCellNum();
try{
cell = row.getCell((short) 0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
name = cell.getStringCellValue().trim();
name=new String(name.getBytes( "GBK "), "latin1 ");
}catch(Exception e){
}
try{
cell = row.getCell((short) 1);
aa = (long) cell.getNumericCellValue();
}catch(Exception e){
}
try{
cell = row.getCell((short) 2);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
company = cell.getStringCellValue().trim();
company=new String(company.getBytes( "GBK "), "latin1 ");
}catch(Exception e){
}
String sql = "insert into listuser(username,company) values( ' "+name+ " ', ' "+company+ " ') ";
try {
stmt.executeUpdate(sql);
} catch (SQLException e1) {
e1.printStackTrace();
return false;
}
}
return true;
}
}
-------------------------------------------------------
调用POI的JSP页面,并删除EXCEL文件
<%@page contentType= "text/html;charset=GB2312 "%>
<%@ page import= "java.io.* "%>
<%@ page import= "poi.POITest "%>
<%
String userid = session.getAttribute( "username ").toString();
boolean connectToDB = POITest.connectDB2();
if(connectToDB){
boolean tureOrfalse = POITest.readExcelToDB2(userid);
if (tureOrfalse==true){
out.println( "数据导入成功 ");
}else{
out.println( "数据导入失败 ");
}
}else{
out.println( "连接数据库失败! ");
}
File file = new File( "E:/Tomcat -------删除EXCEL文件5.0/webapps/sms_cnc_rs/upload/ "+userid+ ".xls ");
file.delete();
%>
----------------------------------------------
问题,EXCEL数据导入数据库后,无法删除此EXCEL文件,手动删除的时候提示正在被使用,数据导入完毕后需要过大概3分后,才能手动删除!请问是怎么回事,请帮忙看看代码,是不是代码中欧中有错误,还是POI CLASS循环中有问题,请指教!
我想读取EXCEL导入数据库后就删除此EXCEL文件,请问应该怎么改才能实现?
[解决办法]
因为你没有关闭读文件的流,所以直到jvm把fs回收了以后才释放了这个文件,就可以删掉了
POIFSFileSystem fs = null;
FileInputStream xxx = null;
try {
xxx = new FileInputStream( "E:\\Tomcat 5.0\\webapps\\sms_cnc_rs\\upload\\ "+userName+ ".xls ");
fs = new POIFSFileSystem(xxx);
...
}
最后去执行一下xxx.close();释放读取文件的流,应该就可以删掉了