遇到将二进制数据显示出来变成 PDF 文件的问题。 其实就是把二进制数据,使用 FileOutputStream 流写入文件之中。
DB2 数据库中是Blob数据类型 ,需要 转换成 byte[] 二进制。
public static byte[] ePolicyDBlobToByte(TPolicyElectron policyEl) {
BufferedInputStream is = null;
byte[] bytes = null;
try {
is = new BufferedInputStream(policyEl.getPolicyContent()
.getBinaryStream());
bytes = new byte[(int) policyEl.getPolicyContent().length()];
int len = bytes.length;
int offset = 0;
int read = 0;
while (offset < len
&& (read = is.read(bytes, offset, len - offset)) >= 0) {
offset += read;
}
} catch (Exception e) {
log.error("Hm接口错误:PDF转换Byte错误" + e);
return null;
}
return bytes;
}
byte[] ePolicyPdf = HmTempPersistent.ePolicyDBlobToByte(xx);
try {
FileOutputStream out = new FileOutputStream(new File("D:\\test.pdf"));
out.write(ePolicyPdf);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}