用java从mysql数据库中读取docx文件

用java从mysql数据库中读取docx文件,java,apache,file,docx,Java,Apache,File,Docx,我有一个将docx文件存储到MYSQL数据库MEDIUMBLOB数据类型的程序 如何从数据库中检索该文件,然后使用ApachePOI读取该文件以检查字数、段落数等?(初始问题) 这是我保存文件的代码 public void saveFile(String FileDirectory, String FileName) { try { //Connecting to MYSQL Database Class.forName(driver).newInstan

我有一个将docx文件存储到MYSQL数据库MEDIUMBLOB数据类型的程序

如何从数据库中检索该文件,然后使用ApachePOI读取该文件以检查字数、段落数等?(初始问题)

这是我保存文件的代码

public void saveFile(String FileDirectory, String FileName)
{
    try {
        //Connecting to MYSQL Database
        Class.forName(driver).newInstance();
        con = DriverManager.getConnection(url+dbName,userName,password);

         //saving the image
        PreparedStatement psmnt = (PreparedStatement) con.prepareStatement("INSERT INTO doccompfiles VALUES(?,?,?)");
        psmnt.setInt(1, 5);
        psmnt.setNString(2, FileName);
        File f = new File(FileDirectory + FileName);
        psmnt.setBlob(3, new FileInputStream(f), f.length());
        psmnt.executeUpdate();

        con.close();
        }
    catch (Exception e) {
          JOptionPane.showMessageDialog(null, "Error saving file");
          e.printStackTrace();
     }

}
研究并找到了一种方法,但我的stacktrace出现了错误。以下是用于检索的代码

public void loadFile(String FileName)
{
    try
    {
        //Connecting to MYSQL Database
        Class.forName(driver).newInstance();
        con = DriverManager.getConnection(url+dbName,userName,password);

        Statement stmt = (Statement) con.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * FROM doccompfiles WHERE FileName = '"+ FileName +"'");
        InputStream is = rs.getBinaryStream("File");

        HWPFDocument doc = new HWPFDocument(is);
        WordExtractor we = new WordExtractor(doc);

        String[] paragraphs = we.getParagraphText();
        JOptionPane.showMessageDialog(null, "Number of Paragraphs" + paragraphs.length);
        con.close();
    }
    catch(Exception ex)
    {
        ex.printStackTrace();
    }
}
堆栈跟踪:

java.sql.SQLException: Before start of result set
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
    at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:854)
    at com.mysql.jdbc.ResultSetImpl.getBinaryStream(ResultSetImpl.java:1553)
    at com.mysql.jdbc.ResultSetImpl.getBinaryStream(ResultSetImpl.java:1586)
    at documentComparisor.Database.loadFile(Database.java:150)
    at documentComparisor.Home$5.actionPerformed(Home.java:195)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
我可以验证该文件是否存储在数据库中,以及select语句中我的表详细信息是否正确。有什么想法吗

谢谢

  • 从表中选择您的BLOB (在处理大文件时查找getBinaryStream,否则为getBlob)

  • 处理它:

    HWPFDocument doc = new HWPFDocument(binaryStream);
    WordExtractor we = new WordExtractor(doc);
    
    String[] paragraphs = we.getParagraphText();
    System.out.println("Total Paragraphs: "+paragraphs.length);
    
    (then iterate through paragraphs array and count words)
    
  • 从表中选择您的BLOB (在处理大文件时查找getBinaryStream,否则为getBlob)

  • 处理它:

    HWPFDocument doc = new HWPFDocument(binaryStream);
    WordExtractor we = new WordExtractor(doc);
    
    String[] paragraphs = we.getParagraphText();
    System.out.println("Total Paragraphs: "+paragraphs.length);
    
    (then iterate through paragraphs array and count words)
    

  • 请检查下面的代码,以从数据库读取和写入blob数据

    公共类ReadBlobData{
    公共静态void main(字符串[]args){
    writeBlob(65,“Test.docx”);
    readBlob(65,“Test.docx”);
    }
    公共静态void readBlob(int-candidateId,字符串文件名){
    String selectSQL=“从scope1.documents中选择文档\文本,其中id=?”;
    结果集rs=null;
    try(Connection conn=mysqljdbcuti.getConnection();
    PreparedStatement pstmt=conn.prepareStatement(selectSQL);){
    //设置参数;
    pstmt.setInt(1,候选ID);
    rs=pstmt.executeQuery();
    //将二进制流写入文件
    文件=新文件(文件名);
    FileOutputStream输出=新的FileOutputStream(文件);
    System.out.println(“写入文件”+file.getAbsolutePath());
    while(rs.next()){
    InputStream输入=rs.getBinaryStream(“文档文本”);
    byte[]buffer=新字节[(int)file.length()];
    while(输入读取(缓冲区)>0){
    输出。写入(缓冲区);
    }
    }
    }catch(SQLException | IOException e){
    System.out.println(e.getMessage());
    }最后{
    试一试{
    如果(rs!=null){
    rs.close();
    }
    }捕获(SQLE异常){
    System.out.println(e.getMessage());
    }
    }
    }
    公共静态void writeBlob(int-candidateId,字符串文件名){
    //更新sql
    字符串updateSQL=“更新范围1.documents”+
    “设置文档_text=?其中id=?”;
    try(Connection conn=mysqljdbcuti.getConnection();
    PreparedStatement pstmt=conn.prepareStatement(updateSQL)){
    //读文件
    File File=新文件(“/home/hardik/Desktop/Test.docx”);
    FileInputStream输入=新的FileInputStream(文件);
    System.out.println(file.length());
    //设置参数
    pstmt.setBinaryStream(1,输入);
    pstmt.setInt(2,候选ID);
    //将简历文件存储在数据库中
    System.out.println(“读取文件”+file.getAbsolutePath());
    pstmt.executeUpdate();
    System.out.println(“将文件存储在数据库中”);
    }catch(SQLException | FileNotFoundException e){
    System.out.println(e.getMessage());
    }
    }
    }
    公共类mysqljdbcuti{
    公共静态连接getConnection()引发SQLException{
    连接conn=null;
    //分配数据库参数
    String url=“jdbc:mysql://localhost:3306/scope1" ;
    字符串user=“root”;
    字符串password=“root”;
    //创建到数据库的连接
    conn=DriverManager.getConnection(url、用户、密码);
    返回连接;
    }
    }
    
    请检查下面的代码,查看数据库中的读写blob数据

    公共类ReadBlobData{
    公共静态void main(字符串[]args){
    writeBlob(65,“Test.docx”);
    readBlob(65,“Test.docx”);
    }
    公共静态void readBlob(int-candidateId,字符串文件名){
    String selectSQL=“从scope1.documents中选择文档\文本,其中id=?”;
    结果集rs=null;
    try(Connection conn=mysqljdbcuti.getConnection();
    PreparedStatement pstmt=conn.prepareStatement(selectSQL);){
    //设置参数;
    pstmt.setInt(1,候选ID);
    rs=pstmt.executeQuery();
    //将二进制流写入文件
    文件=新文件(文件名);
    FileOutputStream输出=新的FileOutputStream(文件);
    System.out.println(“写入文件”+file.getAbsolutePath());
    while(rs.next()){
    InputStream输入=rs.getBinaryStream(“文档文本”);
    byte[]buffer=新字节[(int)file.length()];
    while(输入读取(缓冲区)>0){
    输出。写入(缓冲区);
    }
    }
    }catch(SQLException | IOException e){
    System.out.println(e.getMessage());
    }最后{
    试一试{
    如果(rs!=null){
    rs.close();
    }
    }捕获(SQLE异常){
    System.out.println(e.getMessage());
    }
    }
    }
    公共静态void writeBlob(int-candidateId,字符串文件名){
    //更新sql
    字符串updateSQL=“更新范围1.documents”+
    “设置文档_text=?其中id=?”;
    try(Connection conn=mysqljdbcuti.getConnection();
    PreparedStatement pstmt=conn.prepareStatement(updateSQL)){
    //读文件
    File File=新文件(“/home/hardik/Desktop/Test.docx”);
    FileInputStream输入=新的FileInputStream(文件);
    System.out.println(file.length());