为什么上传后的excel POI无法读取,无法导入数据库。不上传的excel POI就可以读取和导入数据库呢?请看源程序!
以下是全部代码!
文件上传提交页面UploadFileExample1.jsp
------------------------------
<%@ page contentType= "text/html; charset=gb2312 " language= "java " import= "java.sql.* "
errorPage= " " %>
<html>
<head>
<title> 文件上传 </title>
</head>
<body>
<form name= "form1 " method= "post " action= "AcceptUploadFile.jsp " enctype= "multipart/form-data ">
upload file:
<label>
<input type= "file " name= "upfile " size= "50 " />
</label>
<p>
<label>
<input type= "submit " name= "Submit " value= "提交 " />
</label>
</p>
</form>
</body>
</html>
--------------------------------------------------
文件上传处理页面AccepteUploadFile.jsp
----------------------------------------
<%@ page contentType= "text/html; charset=gb2312 " %>
<%@ page import= "java.io.* "%>
<%!
public String codeToString(String str)
{
String s=str;
try
{
byte tempB[]=s.getBytes( "gb2312 ");
s=new String(tempB);
return s;
}
catch(Exception e)
{
return s;
}
}
%>
<%
String userid = session.getAttribute( "username ").toString();
String tempFileName=new String( "tempFileName1 ");//接收上传的文件内容的临时文件的文件名
File tempFile1=new File( "c:/ ",tempFileName);
FileOutputStream outputFile1=new FileOutputStream(tempFile1);
InputStream fileSource1=request.getInputStream();//得到客户端提交的所有数据
byte b[]=new byte[1000];
int n;
while((n=fileSource1.read(b))!=-1)
{
outputFile1.write(b,0,n); //将得到的客户端数据写入临时文件
}
outputFile1.close();
fileSource1.close();
RandomAccessFile randomFile1=new RandomAccessFile(tempFile1, "r ");
randomFile1.readLine();//读取第一行数据
String FilePath=randomFile1.readLine();//读取第二行数据,这行数据包括了文件的路径和文件名
int position=FilePath.lastIndexOf( '\\ '); //等到文件名
String filename=codeToString(FilePath.substring(position+1,FilePath.length()-1));
randomFile1.seek(0);//重新定位指针到文件头
long forthEnterPosition=0;
int forth=1; //得到第4行回车符号的位置,这是上传文件的开始位置
while((n=randomFile1.readByte())!=-1&&(forth <=4))
if(n== '\n ')
{
forthEnterPosition=randomFile1.getFilePointer();
forth++;
}
File FileUploadDir=new File( "E:/Tomcat 5.0/webapps/sms_cnc_rs ", "upload ");
FileUploadDir.mkdir(); //生成上传文件的目录
File saveFile1=new File( "E:/Tomcat 5.0/webapps/sms_cnc_rs/upload ",userid+ ".xls ");
RandomAccessFile randomFile2=new Rando