1

I have a java code which parses a PDF. But it loads the PDF locally. But I now want it to source the pdf from a url, say "https://www.abc.com/xyz.pdf" instead of "C:\xyz.pdf", if I just change strings, it throws error.

URL URL = new URL("https://www.abc.com/xyz.pdf");
InputStream in = URL.openStream();
FileOutputStream fos = new FileOutputStream(new File(temp.pdf));
int length = -1;
    byte[] buffer = new byte[1024];// buffer for portion of data from
    // connection
    while ((length = in.read(buffer)) > -1) {
        fos.write(buffer, 0, length);
    }
fos.close();
in.close();

Also, I get java.net.UnknownHostException when I try the above code at line 2. The link works fine in a browser.

Alexis Pigeon
  • 7,054
  • 11
  • 36
  • 44
raka
  • 305
  • 1
  • 4
  • 12
  • Do you want to place your pdf to a server? If so, [use Java I/O to upload your pdf file to the server](http://stackoverflow.com/questions/2469451/upload-files-with-java). –  Jun 27 '14 at 06:25
  • Where's your code? If you read it from remote, you have to use another API: http://stackoverflow.com/questions/6259339/how-to-read-a-text-file-directly-from-internet-using-java – Smutje Jun 27 '14 at 06:33

1 Answers1

2

In java if you want to read directly from URL, you can use this:

URL oracle = new URL("http://www.oracle.com/");
BufferedReader in = new BufferedReader(
new InputStreamReader(oracle.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
    System.out.println(inputLine);
in.close();

Basically you have to use the URL.openstream method

Bruno Lowagie
  • 71,091
  • 10
  • 98
  • 149
user3309301
  • 301
  • 1
  • 4
  • I've updated the answer. The link is OK, but StackOverflow doesn't allow answers that require a user to click on the link to get the solution. That's why I copy/pasted the relevant code snippet. I upvoted the answer, because it's indeed a good answer (to a question that wasn't related to iText in any way). – Bruno Lowagie Jun 27 '14 at 07:00
  • Considering that the original question referred to the download of a PDF (i.e. a file to be treated as binary, not as text), though, code involving an `InputStreamReader` and a `BufferedReader` does not really seem helpful. I'm actually quite surprised to see that this is the accepted answer. – mkl Jun 27 '14 at 08:06