一、先将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();
}
}