无法用java下载pdf

时间:2012-10-18 22:58:05

标签: java pdf download

我正在尝试从中下载文件 http://aula.au.dk/main/document/document.php?action=download&id=%2F%D8velsesvejledning+2012.pdf 但当我尝试使用此代码下载时,它似乎不是一个pdf

import java.io.*;
import java.net.*;

public class DownloadFile {

public static void download(String address, String localFileName) throws IOException {
    URL url1 = new URL(address);

    byte[] ba1 = new byte[1024];
    int baLength;
    FileOutputStream fos1 = new FileOutputStream(localFileName);

    try {
        // Contacting the URL
        System.out.print("Connecting to " + url1.toString() + " ... ");
        URLConnection urlConn = url1.openConnection();

        // Checking whether the URL contains a PDF
        if (!urlConn.getContentType().equalsIgnoreCase("application/pdf")) {
            System.out.println("FAILED.\n[Sorry. This is not a PDF.]");
        } else {
            try {

                // Read the PDF from the URL and save to a local file
                InputStream is1 = url1.openStream();
                while ((baLength = is1.read(ba1)) != -1) {
                    fos1.write(ba1, 0, baLength);
                }
                fos1.flush();
                fos1.close();
                is1.close();


            } catch (ConnectException ce) {
                System.out.println("FAILED.\n[" + ce.getMessage() + "]\n");
            }
        }

    } catch (NullPointerException npe) {
        System.out.println("FAILED.\n[" + npe.getMessage() + "]\n");
    }
}
}

你能帮助我吗?

3 个答案:

答案 0 :(得分:1)

http://aula.au.dk/main/document/document.php?action=download&id=%2F%D8velsesvejledning+2012.pdf不是pdf。该网站提供错误,这就是脚本不起作用的原因:

  

文件/data/htdocs/dokeos184/www/main/inc/tool_navigation_menu.inc.php第70行的SQL错误

答案 1 :(得分:0)

正如Marti所说,问题的根本原因是脚本失败了。我在一个有效的pdf链接上测试了你的程序,它运行得很好。

在这种情况下,这对你没有帮助,但HttpURLConnection是URLConnection的专用子类,它使得与http服务器的通信变得更加容易 - 例如直接访问错误代码等。

HttpURLConnection urlConn = (HttpURLConnection) url1.openConnection();
// check the responsecode for e.g. errors (4xx or 5xx) 
int responseCode = urlConn.getResponseCode();

答案 2 :(得分:0)

包含2个库的2步流程。

// 1. Use Jsoup to get the response.
Response response= Jsoup.connect(location)
                   .ignoreContentType(true)
                   // more method calls like user agent, referer, timeout 
                   .execute();

// 2. Use Apache Commons to write the file 
FileUtils.writeByteArrayToFile(new File(path), response.bodyAsBytes());