简体   繁体   English

解码base64编码值

[英]decode base64 encoded value

I have content of a file (x509crl) in base64 encoded value. 我具有以base64编码的值表示的文件(x509crl)的内容。 I fired GET request to a servlet. 我向Servlet发出了GET请求。 By pasting following URL in a browser file download dialog pops up. 通过在浏览器文件中粘贴以下URL,将弹出下载对话框。

"http://host:port/myServlet?content=base64_encoded_value"

I was able to download a file, but I could not open a file. 我可以下载文件,但是无法打开文件。 According to a pki expert in my workshop, if content of crl file is correct then OS should open it up just fine. 根据我的工作室的pki专家的说法,如果crl文件的内容正确,则OS应该可以正常打开它。

Here is the doGet method from my servlet class. 这是我的servlet类中的doGet方法。 I wonder what mistakes I made. 我想知道我犯了什么错误。

protected void doGet(HttpServletRequest request, HttpServletResponse response) {
    String content = request.getParameter("content");
    response.setContentType("application/pkix-crl");
    response.setHeader("Content-Disposition","attachment;filename=test.crl");
    byte[] byteArray = Base64.decodeBase64(content.getBytes());
    ServletOutputStream sos = response.getOutputStream();
    sos.write(byteArray);
    sos.close();
}

I am using commons-codec-1.3 and I didn't include any exception handling in the example. 我正在使用commons-codec-1.3,并且在示例中未包含任何异常处理。

So you send the request: 因此,您发送请求:

http://host:port/myServlet?content=base64_encoded_value

which is consumed by the method in your servlet: 它由servlet中的方法消耗:

protected void doGet(HttpServletRequest request, HttpServletResponse response) {

Lets have a look at what you are doing: 让我们看看您在做什么:

    String content = request.getParameter("content");

Seems the String content will now contain "base64_encoded_value" Next, tell the client what we are sending, seems reasonable. 似乎字符串内容现在将包含“ base64_encoded_value”。接下来,告诉客户端我们正在发送什么,似乎是合理的。

    response.setContentType("application/pkix-crl");
    response.setHeader("Content-Disposition","attachment;filename=test.crl");

Then we do this: 然后我们这样做:

    byte[] byteArray = Base64.decodeBase64(content.getBytes());

This seems to be trying to convert the string "base64_encoded_value" to bytes and then decode them. 这似乎是试图将字符串“ base64_encoded_value”转换为字节,然后对其进行解码。 They are not encoded base64, so byteArray probably contains nothing. 它们未编码为base64,因此byteArray可能不包含任何内容。 The rest of it writes that nothing to an output stream in the response. 它的其余部分在响应中不向输出流写入任何内容。

    ServletOutputStream sos = response.getOutputStream();
    sos.write(byteArray);
    sos.close();
}

I am guessing that you actually want to read a file or create a valid crl on the fly, then encode it and send it on, rather than trying to decode the string parameter. 我猜想您实际上是想即时读取文件或创建有效的crl,然后对其进行编码和发送,而不是尝试对string参数进行解码。

After reading more threads, I realized my problem. 阅读更多线程后,我意识到了我的问题。 The simple logic I posted was working right however when I passed raw base64 string in URL as part of query string, I should have url-encoded the value otherwise data is altered when the servlet receives it. 我发布的简单逻辑工作正常,但是当我在URL中将原始base64字符串作为查询字符串的一部分传递时,我应该对该值进行url编码,否则当servlet接收到数据时,数据将被更改。 After I url-encoded base64 string, I was able to download a valid crl. 使用url编码的base64字符串后,我可以下载有效的crl。

The following thread gave me an answer. 以下线程给了我一个答案。

Passing base64 encoded strings in URL 在URL中传递base64编码的字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM