从java中的二进制数据创建pdf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23747982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
create pdf from binary data in java
提问by OJVM
I'm getting this string from a web service.
我从网络服务获取这个字符串。
"JVBERi0xLjQKMyAwIG9iago8PC9UeXBlIC9QYWdlCi9QYXJlbnQgMSAwIFIKL1Jlc291cmNlcyAyIDAgUgovR3JvdXAgPDwvVHlwZSAvR3JvdXAgL1MgL1RyYW5zcGFyZW5jeSAvQ1MgL0RldmljZVJHQj4"
" JVBERi0xLjQKMyAwIG9iago8PC9UeXBlIC9QYWdlCi9QYXJlbnQgMSAwIFIKL1Jlc291cmNlcyAyIDAgUgovR3JvdXAgPDwvVHlwZSAvR3JvdXAgL1MgL1WdlCi9QYXJlbnQgMSAwIFIKL1Jlc291cmNlcyAyIDAgUgovR3JvdXAgPDwvVHlwZSAvR3JvdXAgL1MgL1WdlCi9QYXJlbnQgMSAwIFIKL1Jlc291cmNlcyAyIDAgUgovR3JvdXAgPDwvVHlwZSAvR3JvdXAgL1MgL1WdlCi9QYXJlbnQgMSAwIFIKL1Jlc291MjWdXAgL1MgL1WdlCiRyL1MgL1JFyRyVlz
It is supposed to be a pdf file, i tried this library pdfboxfrom apache, but it writes the content as a text inside the pdf. I've tried with ByteArrayInputStreambut the pdf created is not valid, corrupted, this is some of the code i've wrote.
它应该是一个 pdf 文件,我从 apache尝试了这个库pdfbox,但它把内容写成 pdf 中的文本。我尝试过ByteArrayInputStream但创建的 pdf 无效,已损坏,这是我编写的一些代码。
public void escribePdf(String texto, String rutaSalida) throws IOException{
byte[] biteToRead = texto.getBytes();
InputStream is = new ByteArrayInputStream(biteToRead );
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(new File(rutaSalida))));
int c;
while((c = is.read()) != -1) {
out.writeByte(c);
}
out.close();
is.close();
}
采纳答案by user3648895
That is Base64 encoded (most probably UTF-8) data, you must decode it before using; such as:
那是 Base64 编码(很可能是 UTF-8)数据,您必须在使用前对其进行解码;如:
import sun.misc.BASE64Decoder;
...
...
BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedBytes = decoder.decodeBuffer(biteToRead);
....
....
Edit: For java >= 1.8, use:
编辑:对于 java >= 1.8,使用:
byte[] decodedBytes = java.util.Base64.getDecoder().decode(biteToRead);
回答by ug_
Your string is definitively base 64 encoded. It translates to
您的字符串绝对是 base 64 编码的。它翻译成
%PDF-1.4
3 0 obj
<</Type /Page
/Parent 1 0 R
/Resources 2 0 R
/Group <</Type /Group /S /Transparency /CS /DeviceRG
which isnt a full pdf file by itself which leads me to belive you have something wrong with the way your reading the data from the server.
它本身并不是一个完整的 pdf 文件,这让我相信你从服务器读取数据的方式有问题。
As of java 6 they added a base 64 converter outside the sun packages.
从 java 6 开始,他们在 sun 包之外添加了一个 base 64 转换器。
byte [] bytes = javax.xml.bind.DatatypeConverte.parseBase64Binary(texto);
new String(bytes, "UTF-8");
回答by Patricio Córdova
[JDK 8]
[JDK 8]
Imports:
进口:
import java.io.*;
import java.util.Base64;
Code:
代码:
// Get bytes, most important part
byte[] bytes = Base64.getDecoder().decode("JVBERi0xLjQKMyAwIG9iago8P...");
// Write to file
DataOutputStream os = new DataOutputStream(new FileOutputStream("output.pdf"));
os.write(bytes);
os.close();