公司业务需求需要把.rtf文件转化成文本文件。
package chuyuanxiaojie;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.StringReader;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.zip.ZipInputStream;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.rtf.RTFEditorKit;
public class Indexer {
public static void main(String[] args) {
for (int i = 0; i < 5; i++)
new Thread(new tRunnable(i)).start();
}
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@IP:1521:ORCL", "账号", "密码");
System.out.println("success");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static Connection getConnectionnyns() {
Connection conn = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@IP:1521:ORCL", "账号", "密码");
System.out.println("success");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static String hex2str(byte[] b1) {
String str = null;
try {
byte[] b2 = null;
ZipInputStream zis = new ZipInputStream(
new ByteArrayInputStream(b1));
while (zis.getNextEntry() != null) {
byte[] tmp = new byte[1024];
int num = -1;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((num = zis.read(tmp, 0, tmp.length)) != -1)
baos.write(tmp, 0, num);
b2 = baos.toByteArray();
baos.flush();
baos.close();
}
zis.close();
DefaultStyledDocument dsd = new DefaultStyledDocument();
new RTFEditorKit().read(new ByteArrayInputStream(b2), dsd, 0);
str = new String(dsd.getText(0, dsd.getLength()).getBytes(
"ISO8859_1"), "gbk");
str = str.replaceAll("[A-Z]{2}\\(\\d{8},\\d,\\d\\)", "");
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
class tRunnable implements Runnable {
public int num;
public tRunnable(int num) {
this.num = num;
}
@Override
public void run() {
try {
Connection conn = new Indexer().getConnection();
Connection connnyns = new Indexer().getConnectionnyns();
int startCount = num * 900000;
int endCount = startCount + 900000;
conn.setAutoCommit(false);
PreparedStatement ps = connnyns
.prepareStatement("insert into medical_record values(?,?,?)");
System.out.println("线程" + num + "开始 " + startCount + "--"
+ endCount);
for (int i = startCount, j = 0; i < endCount; i += 3000) {
ResultSet rs = conn
.createStatement()
.executeQuery(
"select * from (select a.文件ID,a.内容,b.创建时间,rownum rn from 电子病历格式 a,电子病历记录 b where a.文件ID=b.ID and b.文件ID='29' and rownum<="
+ (i + 3000) + ") where rn>" + i);
while (rs.next()) {
Blob blob = rs.getBlob(2);
byte[] b1 = blob.getBytes(1, (int) blob.length());
String s = Indexer.hex2str(b1);
ps.setLong(1, rs.getLong(1));
ps.setCharacterStream(2, new StringReader(s), s.length());
ps.setDate(3, rs.getDate(3));
ps.executeUpdate();
}
conn.commit();
rs.close();
j += 3000;
System.out.println("线程" + num + "完成" + j + "条记录");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}