3

I need to download PDF file from web, for example http://www.math.uni-goettingen.de/zirkel/loesungen/blatt15/loes15.pdf this link. I have to do it using Streams. With images it works fine by me :

public static void main(String[] args) {
        try {           
                //get the url page from the arguments array
                String arg = args[0];
                URL url = new URL("https://cs7065.vk.me/c637923/v637923205/25608/AD8WhOSx1ic.jpg");

                try{
                    //jpg
                    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();
                    FileOutputStream fos = new FileOutputStream("borrowed_image.jpg");
                    fos.write(response);
                    fos.close();
                 }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

But with PDf it does not work. What could be the problem ?

4
  • 2
    Please clarify what "does not work" means. Is the code throwing an exception? Is it generating an unreadable pdf file? Something else? Commented Dec 17, 2016 at 18:39
  • @mangotang, it is creating an unreadble PDF file Commented Dec 17, 2016 at 18:40
  • I fixed the syntax errors in your code, and used this url to get a sample pdf, and your code worked fine: pdf995.com/samples/pdf.pdf Commented Dec 17, 2016 at 18:48
  • Works for me after cleaning up the try-statements. Doesn't look that bad but for lacking doc comments, not using try-with-resource and overdoing buffering. Commented Dec 17, 2016 at 18:57

2 Answers 2

4

I made minor edits to your code to fix syntax errors and, this seems to work (below). Consider placing your close() statements in a finally block.

package org.snb;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;

public class PdfTester {
    public static void main(String[] args) {
        //get the url page from the arguments array

        try{
            //String arg = args[0];
            URL url = new URL("http://www.pdf995.com/samples/pdf.pdf");
            //jpg
            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();
            FileOutputStream fos = new FileOutputStream("/tmp/bart.pdf");
            fos.write(response);
            fos.close();
         }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

try this, this got the job done (and pdf is readable). see if there are any exceptions thrown when requesting the url.

public static void main(String[] args) {
        try {
            //get the url page from the arguments array
            URL url = new URL("http://www.math.uni-goettingen.de/zirkel/loesungen/blatt15/loes15.pdf");

            try {
                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();
                FileOutputStream fos = new FileOutputStream("loes15.pdf");
                fos.write(response);
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.