用到的jar
jacob.jar
http://mvnrepository.com/artifact/net.sf.jacob-project/jacob
public final DataMap queryApplicantDOC2PDF(final DataMap para) throws Exception {
String sbrid = para.getString("sbrid");
StringBuffer sqlBF = new StringBuffer();
sqlBF.setLength(0);
sqlBF.append(" select sbrdoc, upper(zjhm) zjhm ");
sqlBF.append(" from vt.applicant ");
sqlBF.append(" where sbrid = ? ");
this.sql.setSql(sqlBF.toString());
sql.setString(1, sbrid);
DataSet dsApplicant = sql.executeQuery();
if (dsApplicant.size() <= 0) {
return null;
}
String zjhm = dsApplicant.getString(0, "zjhm");
String filePath = "temp" + File.separator + GlobalVars.APP_ID;
String path = filePath + File.separator + zjhm;
File docFile = new File(path + File.separator + zjhm + ".doc");
File docPdfFile = new File(path + File.separator + zjhm + "doc.pdf");
if (!docPdfFile.exists()) {
Blob sbrpdf = dsApplicant.getBlob(0, "sbrdoc");
int len = (new BigDecimal(sbrpdf.length())).intValue();
byte[] byteDoc = sbrpdf.getBytes(1, len);
path = path + File.separator;
File dir = new File(path);
if (!dir.exists()) {
if (!dir.mkdirs()) {
throw new Exception("创建目录失败!");
}
}
FileIOUtil.writeBytesToFile(byteDoc, docFile);
}
try {
app = new ActiveXComponent("Word.Application");
Dispatch documents = app.getProperty("Documents").toDispatch();
Dispatch document = Dispatch.call(documents, "Open", wordFile, false,
true).toDispatch();
File target = new File(docPdfFile);
if (!docPdfFile.exists()) {
Dispatch.call(document, "SaveAs", pdfFile, 17);
Dispatch.call(document, "Close", false);
}
}catch(Exception e) {
System.out.println("转换失败"+e.getMessage());
}finally {
app.invoke("Quit", 0);
}
String filePath = Path + File.separator + zjhm + "doc.pdf"
String fileName = zjhm + "doc.pdf"
FileInputStream inputstream = null;
HttpServletResponse response = null;
try {
inputstream = new FileInputStream(filePath);
FileIOUtil.writeStreamToResponse(inputstream, fileName, response);
} catch (IOException e) {
throw new Exception("文件读取异常:" + e.getMessage());
} finally {
try {
if (inputstream != null) {
inputstream.close();
}
} catch (Exception e) {
throw new Exception("文件损坏或不存在:"
+ e.getMessage());
}
}
return null;
}