我将EXCEL文件上传到服务器后(上传的路径是:C:/Tomcat5/webapps/ROOT/file/upload/),再将EXCEL导入数据库,导入的时候,TOMCAT提示下列错误:java.io.IOException:Unable to read entir block;91 bytes read;expected 512 bytes
at org.apache.poi.poifs.storage.RawDataBlock.<init><RawDataBlock.java:62>
at at org.apache.poi.poifs.storage.RawDataBlockList.<init><RawDataBlockList.java:51>
at at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init><POIFSFileSystem.java:86>
at poi.POITest.readExceIToDB2(POITest.java:46)
at org.apache.jsp.ca_jsp._jspService(ca_jsp.java:51)
at org.apache.jasper.runtime.HttpJspBase.service(HttpjspBase.java:94)
at javax.serlet.http.HttpService(HttpSerlet.java:810)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:298)源程序如下:POITest.java
-------------<%@page contentType="text/html;charset=GB2312"%><%@ 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("连接数据库失败!");
}
%>
------------------
调用CLASS的JSP页面ca.jsp
<%@page contentType="text/html;charset=GB2312"%><%@ 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("连接数据库失败!");
}
%>---------------------------------------------------
请高手指点!我个人觉得是不是由于上传的时候使用了java.io,读取excel的时候也需要java.io,两者冲突的原因?目前不得其解,请指点!非常感谢您的帮忙!
at org.apache.poi.poifs.storage.RawDataBlock.<init><RawDataBlock.java:62>
at at org.apache.poi.poifs.storage.RawDataBlockList.<init><RawDataBlockList.java:51>
at at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init><POIFSFileSystem.java:86>
at poi.POITest.readExceIToDB2(POITest.java:46)
at org.apache.jsp.ca_jsp._jspService(ca_jsp.java:51)
at org.apache.jasper.runtime.HttpJspBase.service(HttpjspBase.java:94)
at javax.serlet.http.HttpService(HttpSerlet.java:810)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:298)源程序如下:POITest.java
-------------<%@page contentType="text/html;charset=GB2312"%><%@ 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("连接数据库失败!");
}
%>
------------------
调用CLASS的JSP页面ca.jsp
<%@page contentType="text/html;charset=GB2312"%><%@ 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("连接数据库失败!");
}
%>---------------------------------------------------
请高手指点!我个人觉得是不是由于上传的时候使用了java.io,读取excel的时候也需要java.io,两者冲突的原因?目前不得其解,请指点!非常感谢您的帮忙!
解决方案 »
- 用JAVA正则表达式获得第二个DIV中的超链接。
- 提交form局部刷新为什么新打开了一个窗口
- Hibernate中保存声明父类中的子类对象
- 翻页如何不让背景音乐中断
- 在页面中使用iframe后 每次进入页面提示contains both secure and nonsecure items。如何解决,急急急!
- 在JSP中,java和JavaScript如何交互?
- 简单乱码问题,我再javabean里有个乱码全角字符“·”
- 请问在java程序中何时必须要用try...catch
- 现在哪里有 tomcat4.1.18下载,非常 着急,马上给分!!!!!!!!!!
- 问一个关于treegrid树形表格的设计思路
- 还是checkbox的问题。怎么在ACCESS中批删数据?看不懂
- 请问如果配置项目的跟目录?
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){}try{
cell = row.getCell((short) 3);
phone = (long) cell.getNumericCellValue();
}catch(Exception e){}
try{
cell = row.getCell((short) 4);
mobile = (long) cell.getNumericCellValue();
}catch(Exception e){}
try{
cell = row.getCell((short) 5);
email = cell.getStringCellValue();
}catch(Exception e){}
try{
cell = row.getCell((short) 6);
zu = cell.getStringCellValue();
}catch(Exception e){}String sql = "insert into listuser(username,password,email,homepage,signs,fenzu,company,mobile) values('"+name+"','"+phone+"','"+email+"','"+aa+"','"+userName+"','"+zu+"','"+company+"','"+mobile+"')";
try {
stmt.executeUpdate(sql);
} catch (SQLException e1) {
e1.printStackTrace();
return false;
}
}
return true;
}
}
fs = new POIFSFileSystem(new FileInputStream("E:\\Tomcat 5.0\\webapps\\sms_cnc_rs\\upload\\"+userName+".xls"));
可能是Excel的版本差异造成的
问题补充:这是我的EXCEL文件上传代码:文件上传提交页面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 RandomAccessFile(saveFile1,"rw");
randomFile1.seek(randomFile1.length());
long endPosition=randomFile1.getFilePointer();//找到上传的文件数据的结束位置,即倒数第4行
int j=1;
while((endPosition>=0)&&(j<=4))
{
endPosition--;
randomFile1.seek(endPosition);
if(randomFile1.readByte()=='\n')
j++;
}
randomFile1.seek(forthEnterPosition);
long startPoint=randomFile1.getFilePointer();
while(startPoint<endPosition-1)
{
randomFile2.write(randomFile1.readByte());
startPoint=randomFile1.getFilePointer();
}
randomFile2.close();
randomFile1.close();
tempFile1.delete();
%>
<% response.sendRedirect("ca.jsp");%> ---------------跳转到excel导入数据库------------------------------------------------------------问题如下:我用poi将excel文件导入数据库,导入前先将excel上传到服务器上,现在上传成功了,但是无法将excel文件导入数据库。也能导入成功,但是需要将上传的excel文件打开,再关闭一下,“刷新一下ca.jsp”就可以将excel文件导入数据库,否则无法导入成功!!请问是怎么回事?