的base64德$ C $光盘文件不等于原始unen $ C $光盘文件 [英] base64 decoded file is not equal to the original unencoded file
问题描述
我有一个正常的PDF文件A.pdf,第三党恩codeS文件中的Base64和它在一个Web服务发送给我作为一个长字符串(我对第三方没有控制)。
我的问题是,当我去code与Java org.apache.commons字符串。codec.binary.Base64和右输出到文件名为B.pdf
我希望B.pdf是相同A.pdf,但B.pdf原来有点不同,那么A.pdf。因此B.pdf不被识别为通过杂技演员有效PDF。
的base64是否有不同类型的编码\\字符集的机制?我能察觉我收到的字符串是如何连接codeD,以便B.pdf = A.pdf?
编辑 - 这就是我要脱code中的文件,解码它应该作为一个PDF打开后
这是文件的头在记事本中打开++
** ** A.pdf
%PDF-1.4
%±²³'
%的创WNV / EP PDF工具V6.1
1 0 OBJ
<<
/ PageMode / UseNone
/浏览器preferences 2 0 R
/类型/目录 ** ** B.pdf
%PDF-1.4
%±²³'
%的创WNV / EP PDF工具V6.1
1 0! BJ
<<
/ PageMode / UseNone
/浏览器preferences 2 0 R
/]
PE /目录
这是我如何去code字符串
私有静态无效德codeStringToFile(字符串连接codedInputStr,
字符串outputFileName)抛出IOException
在的BufferedReader = NULL;
的BufferedOutputStream出= NULL;
尝试{
在=新的BufferedReader(新StringReader(EN codedInputStr));
OUT =新的BufferedOutputStream(新的FileOutputStream(outputFileName));
德codeStream(IN,OUT);
了out.flush();
} {最后
如果(在!= NULL)
附寄();
如果(满分!= NULL)
out.close();
}
} 私有静态无效德codeStream(在的BufferedReader,OutputStream中出)
抛出IOException
而(真){
字符串s = in.readLine();
如果(S == NULL)
打破;
//System.out.println(s);
字节[] buf中= Base64.de codeBase64(S);
out.write(BUF);
} }
-
您是通过工作线由行破坏你的解码。 的Base64 德codeRS干脆忽略空格,这意味着在原有内容的字节很可能井被分解成两个的Base64文本行。你应该串联在一起的所有线条和德code一气呵成文件。
-
preFER使用
字节[]
,而不是字符串
提供内容给<当code>的Base64 类方法。字符串
意味着字符集编码,你想要什么可能做不到。
I have a normal pdf file A.pdf , a third party encodes the file in base64 and sends it to me in a webservice as a long string (i have no control on the third party).
My problem is that when i decode the string with java org.apache.commons.codec.binary.Base64 and right the output to a file called B.pdf I expect B.pdf to be identical to A.pdf, but B.pdf turns out a little different then A.pdf. As a result B.pdf is not recognized as a valid pdf by acrobat.
Does base64 have different types of encoding\charset mechanisms? can i detect how the string I received is encoded so that B.pdf=A.pdf ?
EDIT- this is the file I want to decode, after decoding it should open as a pdf
this is the header of the files opened in notepad++
**A.pdf**
%PDF-1.4
%±²³´
%Created by Wnv/EP PDF Tools v6.1
1 0 obj
<<
/PageMode /UseNone
/ViewerPreferences 2 0 R
/Type /Catalog
**B.pdf**
%PDF-1.4
%±²³´
%Created by Wnv/EP PDF Tools v6.1
1 0! bj
<<
/PageMode /UseNone
/ViewerPreferences 2 0 R
/]
pe /Catalog
this is how I decode the string
private static void decodeStringToFile(String encodedInputStr,
String outputFileName) throws IOException {
BufferedReader in = null;
BufferedOutputStream out = null;
try {
in = new BufferedReader(new StringReader(encodedInputStr));
out = new BufferedOutputStream(new FileOutputStream(outputFileName));
decodeStream(in, out);
out.flush();
} finally {
if (in != null)
in.close();
if (out != null)
out.close();
}
}
private static void decodeStream(BufferedReader in, OutputStream out)
throws IOException {
while (true) {
String s = in.readLine();
if (s == null)
break;
//System.out.println(s);
byte[] buf = Base64.decodeBase64(s);
out.write(buf);
}
}
You are breaking your decoding by working line-by-line. Base64 decoders simply ignore whitespace, which means that a byte in the original content could very well be broken into two Base64 text lines. You should concatenate all the lines together and decode the file in one go.
Prefer using
byte[]
rather thanString
when supplying content to theBase64
class methods.String
implies character set encoding, which may not do what you want.
这篇关于的base64德$ C $光盘文件不等于原始unen $ C $光盘文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!