简体   繁体   中英

How to save a PDF file from the PDF URL of Orbeon form with Java

I tried to save a PDF document from the PDF URL generated by Orbeon with Java, and always my InputStream is null . When I open this URL in the browser it work well but when I inspect the code I don't find the body of the PDF.

try {
    URL urlPDF = new URL(urlPdfOrbeon);

    URLConnection connection = urlPDF.openConnection();

    InputStream in = connection.getInputStream();
    System.out.println(IOUtils.toString(in)) ;
    FileOutputStream fos = new FileOutputStream(newFile("yourFile.pdf"));

    int length = -1;
    byte[] buffer = new byte[1024];
    while ((length = in.read(buffer)) > -1) {
       fos.write(buffer, 0, length);
    }
    fos.close();
    in.close();

}

The input stream is not null. But it doesn't have any byte to read anymore when you try writing its content to the file. And that's expected, since immediately before, you read everything in the stream by calling

System.out.println(IOUtils.toString(in));

EDIT:

If the input stream actually contains an HTML page asking you to log in, it's probably that you need to be logged in to access this PDF.

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