⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wordextractor.java

📁 一个java与数据库连接的好例子
💻 JAVA
字号:
package com.eap;
import java.io.*;
import java.util.ArrayList;
import org.apache.poi.hwpf.HWPFDocument;
import	org.apache.poi.hwpf.usermodel.*;
import	org.apache.poi.hwpf.model.*;
import org.apache.poi.poifs.filesystem.*;
import org.apache.poi.util.LittleEndian;

public class WordExtractor 
{   
    public WordExtractor() {   
    }   
  
    public String extractText(InputStream in) throws IOException {   
        ArrayList text = new ArrayList();   
        POIFSFileSystem fsys = new POIFSFileSystem(in);   
  
        DocumentEntry headerProps = (DocumentEntry) fsys.getRoot().getEntry("WordDocument");   
        DocumentInputStream din = fsys.createDocumentInputStream("WordDocument");   
        byte[] header = new byte[headerProps.getSize()];   
        System.out.println(headerProps.getSize());
        din.read(header);   
        din.close();   
        // Prende le informazioni dall'header del documento   
        int info = LittleEndian.getShort(header, 0xa);   
       
        boolean useTable1 = (info & 0x200) != 0;   
  
        //boolean useTable1 = true;   
           
        // Prende informazioni dalla piece table   
        int complexOffset = LittleEndian.getInt(header, 0x1a2);   
        //int complexOffset = LittleEndian.getInt(header);   
           
        String tableName = null;   
        if (useTable1) {   
            tableName = "1Table";   
        } else {   
            tableName = "0Table";   
        }   
  
        DocumentEntry table = (DocumentEntry) fsys.getRoot().getEntry(tableName);   
        byte[] tableStream = new byte[table.getSize()];   
  
        din = fsys.createDocumentInputStream(tableName);   
  
        din.read(tableStream);   
        din.close();   
  
        din = null;   
        fsys = null;   
        table = null;   
        headerProps = null;   
  
        int multiple = findText(tableStream, complexOffset, text);   
  
        StringBuffer sb = new StringBuffer();   
        int size = text.size();   
        tableStream = null;   
  
        for (int x = 0; x < size; x++) {   
               
            WordTextPiece nextPiece = (WordTextPiece)text.get(x);   
            int start = nextPiece.getStart();   
            int length = nextPiece.getLength();   
  
            boolean unicode = nextPiece.usesUnicode();   
            String toStr = null;   
            if (unicode) {   
                toStr = new String(header, start, length * multiple, "UTF-16LE");   
            } else {   
                toStr = new String(header, start, length, "ISO-8859-1");   
            }   
            sb.append(toStr).append(" ");   
  
        }   
        return sb.toString();   
    }   //end constructor
  
    private static int findText(byte[] tableStream, int complexOffset, ArrayList text)   
        throws IOException {   
        //actual text   
        int pos = complexOffset;   
        int multiple = 2;   
        //skips through the prms before we reach the piece table. These contain data   
        //for actual fast saved files   
        while (tableStream[pos] == 1) {   
            pos++;   
            int skip = LittleEndian.getShort(tableStream, pos);   
            pos += 2 + skip;   
        }   
        if (tableStream[pos] != 2) {   
            throw new IOException("corrupted Word file");   
        } else {   
            //parse out the text pieces   
            int pieceTableSize = LittleEndian.getInt(tableStream, ++pos);   
            pos += 4;   
            int pieces = (pieceTableSize - 4) / 12; 
            System.out.println(pieces);
            for (int x = 0; x < pieces; x++) {   
                int filePos =   
                    LittleEndian.getInt(tableStream, pos + ((pieces + 1) * 4) + (x + 2));   
                boolean unicode = false;   
                if ((filePos & 0x40000000) == 0) {   
                    unicode = true;   
                } else {   
                    unicode = false;   
                    multiple = 1;   
                    filePos &= ~(0x40000000); //gives me FC in doc stream   
                    filePos /= 2;   
                }   
                int totLength =   
                    LittleEndian.getInt(tableStream, pos + (x + 1) * 4)   
                        - LittleEndian.getInt(tableStream, pos + (x * 4));   
  
                WordTextPiece piece = new WordTextPiece(filePos, totLength, unicode);   
                text.add(piece);   
  
            }   
  
        }   
        return multiple;   
    }   
    public static void main(String[] args){   
        WordExtractor w  = new WordExtractor();   
        POIFSFileSystem ps = new POIFSFileSystem();   
        try{   
               
            File file = new File("C:\\2.doc");   
            InputStream in = new FileInputStream(file);   
            String s = w.extractText(in); 
            
            String con="0001号:"+"\r"+
            "    你好! "+"\r"+
            "    这次心理测试结果已出来.从全面的情况来看,你的心理还是比较健康的.我们从抑郁、精神分裂、等四个方面"+
            "行测试,你的每项得分分别是:40,20,18,22,处在正常范围内,望你保持心情愉快!";
            w.writeWordFile("c:\\c.doc", con);
            //System.out.println("asdfasdfa");
            System.out.println(s);   
       
               
        }catch(Exception e){   
            e.printStackTrace();   
        }   
                   
    }  
	
	public boolean writeWordFile(String path, String content) {   
    boolean w = false;   
    try {   
  
    //  byte b[] = content.getBytes("ISO-8859-1");   
        byte b[] = content.getBytes("GB2312");   
           
        ByteArrayInputStream bais = new ByteArrayInputStream(b);   
  
        POIFSFileSystem fs = new POIFSFileSystem();   
        DirectoryEntry directory = fs.getRoot();   
  
        DocumentEntry de = directory.createDocument("WordDocument", bais);   
  
        FileOutputStream ostream = new FileOutputStream(path);   
  
        fs.writeFilesystem(ostream);   
           
        bais.close();   
        ostream.close();   
  
    } catch (IOException e) {   
        e.printStackTrace();   
    }   
  
    return w;   
}   
  
}   //end public class
class WordTextPiece {   
    private int _fcStart;   
    private boolean _usesUnicode;   
    private int _length;   
  
    public WordTextPiece(int start, int length, boolean unicode) {   
        _usesUnicode = unicode;   
        _length = length;   
        _fcStart = start;   
    }   
    public boolean usesUnicode() {   
        return _usesUnicode;   
    }   
  
    public int getStart() {   
        return _fcStart;   
    }   
    public int getLength() {   
        return _length;   
    }   
  
}   

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -