how to read pdf file from http url folder in java

Solutions on MaxInterview for how to read pdf file from http url folder in java by the best coders in the world

showing results for - "how to read pdf file from http url folder in java"
Sabrina
12 Jul 2019
1import java.io.FileOutputStream;
2import java.io.IOException;
3import java.io.InputStream;
4import java.net.ConnectException;
5import java.net.URL;
6import java.net.URLConnection;
7
8import com.gnostice.pdfone.PdfDocument;
9
10
11public class Read_PDF_From_URL {
12
13  public static void main(String[] args) throws IOException {
14
15    URL url1 =
16      new URL("http://www.gnostice.com/downloads/Gnostice_PathQuest.pdf");
17
18    byte[] ba1 = new byte[1024];
19    int baLength;
20    FileOutputStream fos1 = new FileOutputStream("download.pdf");
21
22    try {
23      // Contacting the URL
24      System.out.print("Connecting to " + url1.toString() + " ... ");
25      URLConnection urlConn = url1.openConnection();
26
27      // Checking whether the URL contains a PDF
28      if (!urlConn.getContentType().equalsIgnoreCase("application/pdf")) {
29          System.out.println("FAILED.\n[Sorry. This is not a PDF.]");
30      } else {
31        try {
32
33          // Read the PDF from the URL and save to a local file
34          InputStream is1 = url1.openStream();
35          while ((baLength = is1.read(ba1)) != -1) {
36              fos1.write(ba1, 0, baLength);
37          }
38          fos1.flush();
39          fos1.close();
40          is1.close();
41
42          // Load the PDF document and display its page count
43          System.out.print("DONE.\nProcessing the PDF ... ");
44          PdfDocument doc = new PdfDocument();
45          try {
46            doc.load("download.pdf");
47            System.out.println("DONE.\nNumber of pages in the PDF is " +
48                               doc.getPageCount());
49            doc.close();
50          } catch (Exception e) {
51            System.out.println("FAILED.\n[" + e.getMessage() + "]");
52          }
53
54        } catch (ConnectException ce) {
55          System.out.println("FAILED.\n[" + ce.getMessage() + "]\n");
56        }
57      }
58
59    } catch (NullPointerException npe) {
60      System.out.println("FAILED.\n[" + npe.getMessage() + "]\n");
61    }
62  }
63}
similar questions