我将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,两者冲突的原因?目前不得其解,请指点!非常感谢您的帮忙!

解决方案 »

  1.   

    POITest.java源程序如下,上面复制错了,不好意思!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){}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;
     }
    }
      

  2.   

    fs = new POIFSFileSystem(new FileInputStream("E:\\Tomcat 5.0\\webapps\\sms_cnc_rs\\upload\\"+userName+".xls"));路径好像没错误!
      

  3.   

    POITest.java:46行是哪?
    fs = new POIFSFileSystem(new FileInputStream("E:\\Tomcat 5.0\\webapps\\sms_cnc_rs\\upload\\"+userName+".xls"));
      

  4.   

    把地址打印出来看看,然后用win路径访问试下
      

  5.   

    http://www.mail-archive.com/[email protected]/msg05157.html
    可能是Excel的版本差异造成的
      

  6.   

    li_d_s(鄙视那些不懂Java却跑来乱骂的人,.NET没啥了不起)
    问题补充:这是我的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文件导入数据库,否则无法导入成功!!请问是怎么回事?
      

  7.   

    li_d_s(鄙视那些不懂Java却跑来乱骂的人,.NET没啥了不起)我使用的是excel 2003
      

  8.   

    li_d_s(鄙视那些不懂Java却跑来乱骂的人,.NET没啥了不起)请看一下这个:http://bbs.xml.org.cn/blog/more.asp?name=lhwork&id=17422我按照他的方法改了一下POI的JAR包,结果还是不行!