将word文件的Blob(数据库表中blob字段)转为pdf文件的base64


一、先将Blob转为字节数组

Blob attFile = (Blob) queryFileByPkVal.get("ATT_FILE");
byte[] blobToBytes = Base64Util.blobToBytes(attFile);

public static byte[] blobToBytes(Blob blob) {
BufferedInputStream is = null;
try {
is = new BufferedInputStream(blob.getBinaryStream());
byte[] bytes = new byte[(int) blob.length()];
int len = bytes.length;
int offset = 0;
int read = 0;
while (offset < len
&& (read = is.read(bytes, offset, len - offset)) >= 0) {
offset += read;
}
return bytes;
} catch (Exception e) {
return null;
} finally {
try {
is.close();
is = null;
} catch (IOException e) {
return null;
}
}
}

 

 

三、上代码

public static String wordToPdf(byte[] wordPath) throws IOException {
getLicense();// 去掉水印
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
String base64 = Base64.encode(wordPath);
BASE64Decoder baseDecoder = new BASE64Decoder();
byte[] battfile1 = baseDecoder.decodeBuffer(base64);
InputStream battfile = new ByteArrayInputStream(battfile1);
Document doc = new Document(battfile);
doc.save(out, SaveFormat.PDF);
byte[] bytes = out.toByteArray();
String battfile2 = Base64Util.convertByteArrayToBase64String(bytes);
return battfile2;
} catch (Exception e) {
e.printStackTrace();
}
return "解析失败";
}

private static void getLicense() {
try (InputStream is = InterFilePostServiceImpl.class.getClassLoader().getResourceAsStream("License.xml")) {
License license = new License();
license.setLicense(is);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM