简体   繁体   中英

My base64 encoded byte[] stream has extra characters after sent through a http response

I encode a pdf into a base64 byte[] stream and I want to send it as a http response to the browser. The problem is that the browser fails to load pdf.

I compared the base 64 string which I printed into the IDE console and the one from the browser console. The one from the IDE console is correct and the one from the browser has extra characters.

So, my base64 byte[]stream gets broken somehow when it's sent as a http response ? How do I solve this?

Le : The code

    FileInputStream inputFileInputStream = null;
    ServletOutputStream outputFileOutputStream = null;

    File exportFile = new File(exportedReport);
    int fileSize = (int) exportFile.length();
    String fullPathToExport = exportFile.getAbsolutePath();
    File fullPathFile = new File(fullPathToExport);

    try {
        // test to see if the path of the file is correct
        System.out.println("The file is located at: "
                + exportFile.getAbsolutePath());

        response.reset();
        response.setContentType("application/pdf");
        response.setContentLength(fileSize);
        response.addHeader("Content-Transfer-Encoding", "base64");
        response.setHeader( "Content-Disposition", "inline; filename=\"" + exportedReport +"\"");


        inputFileInputStream = new FileInputStream(fullPathFile);
        outputFileOutputStream = response.getOutputStream();

        if (bytesToRead == -1) {
            bytesToRead = (int)fullPathFile.length();
        }
        byte[] buffer = new byte[bytesToRead];
        int bytesRead = -1;

        while((inputFileInputStream != null) && ((bytesRead = inputFileInputStream.read(buffer)) != -1)){ 

            if (codec.equals("base64")) {
                //outputFileOutputStream.write(Base64.encodeBytes(buffer).getBytes("UTF-8"), 0, bytesToRead);

                outputFileOutputStream.write(org.apache.commons.codec.binary.Base64.encodeBase64(buffer));

            } else {
                outputFileOutputStream.write(buffer, 0, bytesToRead);
            }
        }
        inputFileInputStream.close();
        outputFileOutputStream.flush();
        outputFileOutputStream.close();

Your code has one major problem:

You are not sending one base64 encoded data part but many base64 encoded data parts (concatenated). But two or more base64 encoded data parts are not equal to one base64 encoded data part.

Example:

base64("This is a test") -> "VGhpcyBpcyBhIHRlc3Q="
base64("This ")+base64("is a ")+base64("test") -> "VGhpcyA=aXMgYSA=dGVzdA=="

You should use the org.apache.commons.codec.binary.Base64InputStream instead of the Base64.encodeBase64() utility method. Read the whole FileInputStream through it and you will get a valid base64 encoded data stream.

Anyway what you are doing is not necessary. You can transfer any 8 bit data via HTTP without further encoding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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