简体   繁体   中英

Java - Obtain PDF from URL and return BASE64 string

I have the following task to obtain a PDF from URL and return a BASE64 string.

What I have currently (sorry I am not a Java Expert):

public String readPDFSOAP(String var, Container container) throws StreamTransformationException{
try {
        //get the url page from the arguments array
        URL url = new URL("URLPDF");
        try {
            //get input Stream from URL
                            InputStream in = new BufferedInputStream(url.openStream());
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            byte[] buf = new byte[131072];
            int n = 0;
            while (-1 != (n = in.read(buf))) {
                out.write(buf, 0, n);
            }
            out.close();
            in.close();
            byte[] response = out.toByteArray();
                            String string = new String(response);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }return String;}

But the string can't be returned. Any help is appreciated.

Thanks, Julian

Your code is all kinds of wrong. For starters, use the Base64 class to handle encoding your byte array. And no need to assign it to a variable, just return it.

return Base64.getEncoder().encodeToString(response)

and on your last line, outside of your try/catch block, just throw an exception. If you get there then you weren't able to properly retrieve and encoded the response, so no need to return a value. You're in an error condition.

Use java.util.Base64 .

PDFs can be pretty large. Instead of reading it into memory, encode the InputStream directly:

ByteArrayOutputStream out = new ByteArrayOutputStream();

try (InputStream in = new BufferedInputStream(url.openStream())) {
    in.transferTo(Base64.getEncoder().wrap(out));
}

String base64 = out.toString(StandardCharsets.US_ASCII);

The Base64 encoded version is even larger than the original file. I don't know what you plan to do with the encoded version, but if you're planning to write it somewhere, you want to avoid keeping any version of the file—original or encoded—in memory. You can do that by having your method accept an OutputStream as an argument:

public void readPDFSOAP(OutputStream destination,
                        String var,
                        Container container)
throws StreamTransformationException,
       IOException {

    URL url = new URL("https://example.com/doc.pdf");
    try (InputStream in = new BufferedInputStream(url.openStream())) {
        in.transferTo(Base64.getEncoder().wrap(destination));
    }
}

Update:

Since you have said you cannot use a try-with-resources statement:

A try-with-resources statement is just a convenient way to guarantee an InputStream (or other closeable resource) is closed. This:

try (InputStream in = new BufferedInputStream(url.openStream())) {
    // code that uses 'in'
}

is (nearly) equivalent to this:

InputStream in = null;

try {
    in = new BufferedInputStream(url.openStream());
    // code that uses 'in'
} finally {
    if (in != null) {
        try {
            in.close();
        } catch (IOException e) {
            // Suppress
        }
    }
}

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