Base64 decode in C# or Java(C# 或 Java 中的 Base64 解码)
问题描述
我有一个带有以下标头的 Base64 编码对象:
I have a Base64-encoded object with the following header:
application/x-xfdl;content-encoding="asc-gzip"
继续解码对象的最佳方法是什么?我需要剥离第一行吗?另外,如果我把它变成一个字节数组(byte[]),我该如何解压缩呢?
What is the best way to proceed in decoding the object? Do I need to strip the first line? Also, if I turn it into a byte array (byte[]), how do I un-gzip it?
谢谢!
我想我一开始说错了.通过说标题是
I think I misspoke initially. By saying the header was
application/x-xfdl;content-encoding="asc-gzip"
我的意思是这是文件的第一行.那么,为了使用 Java 或 C# 库来解码文件,是否需要剥离这一行?
I meant this was the first line of the file. So, in order to use the Java or C# libraries to decode the file, does this line need to be stripped?
如果是这样,去除第一行的最简单方法是什么?
If so, what would be the simplest way to strip the first line?
推荐答案
我能够使用以下代码将 .xfdl 文档转换为 Java DOM 文档.
I was able to use the following code to convert an .xfdl document into a Java DOM Document.
我使用 iHarder 的 Base64 实用程序进行 Base64 解码.
I used iHarder's Base64 utility to do the Base64 Decode.
private static final String FILE_HEADER_BLOCK =
"application/vnd.xfdl;content-encoding="base64-gzip"";
public static Document OpenXFDL(String inputFile)
throws IOException,
ParserConfigurationException,
SAXException
{
try{
//create file object
File f = new File(inputFile);
if(!f.exists()) {
throw new IOException("Specified File could not be found!");
}
//open file stream from file
FileInputStream fis = new FileInputStream(inputFile);
//Skip past the MIME header
fis.skip(FILE_HEADER_BLOCK.length());
//Decompress from base 64
Base64.InputStream bis = new Base64.InputStream(fis,
Base64.DECODE);
//UnZIP the resulting stream
GZIPInputStream gis = new GZIPInputStream(bis);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(gis);
gis.close();
bis.close();
fis.close();
return doc;
}
catch (ParserConfigurationException pce) {
throw new ParserConfigurationException("Error parsing XFDL from file.");
}
catch (SAXException saxe) {
throw new SAXException("Error parsing XFDL into XML Document.");
}
}
仍在努力成功地修改和重新编码文档.
Still working on successfully modifying and re-encoding the document.
希望这会有所帮助.
这篇关于C# 或 Java 中的 Base64 解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C# 或 Java 中的 Base64 解码
基础教程推荐
- 降序排序:Java Map 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01