0

I am trying to download the following url as pdf file in local folder.

http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=1498781&tag=1

so far what i have tried is:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class DownloadPDF {

    public DownloadPDF () {}


    public static String download (String link, String i) {
         String save = null;

         try{
             String file = link;
             URL url = new URL(file);
             InputStream in = url.openStream();
             save = "local_folder/name.pdf";    
             FileOutputStream fos = new FileOutputStream(new File(save));

             int length = -1;
             byte[] buffer = new byte[1024];

            while ((length = in.read(buffer)) > -1) {
                 fos.write(buffer, 0, length);
            }
            fos.close();
            in.close();
            System.out.println("file is downloaded");

       } catch (IOException e) {
            e.printStackTrace();
       }
       return save;
    }  
}

This works for:

http://ongambling.org/wp-content/uploads/2011/02/binde-2005-jgs-postprint.pdf

but does not work for the first url given above? is there any other suggestion?

Parth Satra
  • 513
  • 2
  • 16
mlee_jordan
  • 772
  • 4
  • 18
  • 50

3 Answers3

4

The first URL does not correspond to a pdf file. When you actually go to the page you are simply given the choice to sign-in or purchase, in the form of a button. Now if, by chance, you have bought the pdf and therefore the page re-directs you to the online version of it; you are going to have to take a look at authenticating on the web-page through Java. Such a question has been Discussed before.

Community
  • 1
  • 1
T-Fowl
  • 704
  • 4
  • 9
0

You can't simply download an HTML page and save it with .pdf to convert it to PDF. You have to use a library or somme thing to convert your HTML page to PDF.

It works for the second url because it's already a PDF so you can save it as you download it.

Emrys Myrooin
  • 2,179
  • 14
  • 39
-1

you render your pdf upon hitting your url, that would be the best option. or else check for the param then

try this

            ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
            hwb.write(outByteStream);
            byte [] outArray = outByteStream.toByteArray();
            response.setContentType("application/pdf");
            response.setContentLength(outArray.length);
            response.setHeader("Expires:", "0"); // eliminates browser caching
            response.setHeader("Content-Disposition", "attachment; filename="+fname);
            OutputStream outStream = response.getOutputStream();
            outStream.write(outArray);
            outStream.flush();
            response.sendRedirect("servlet?id=2");
sumanta
  • 359
  • 3
  • 7