Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t使用java下载pdf_Java_Pdf_Download - Fatal编程技术网

Can';t使用java下载pdf

Can';t使用java下载pdf,java,pdf,download,Java,Pdf,Download,我正试图从中下载一个文件 但当我尝试用此代码下载它时,它似乎不是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[

我正试图从中下载一个文件 但当我尝试用此代码下载它时,它似乎不是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");
    }
}
}

你能帮我一下吗?

正如Marti所说,问题的根本原因是脚本失败。我在一个正常工作的pdf链接上测试了你的程序,效果很好

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

HttpURLConnection urlConn = (HttpURLConnection) url1.openConnection();
// check the responsecode for e.g. errors (4xx or 5xx) 
int responseCode = urlConn.getResponseCode();
不是pdf格式。网站出现错误这就是脚本无法工作的原因:

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


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());

网站上好像有个错误。如果你现在点击链接,你就要下载pdf了。你知道我怎么下载这个文件吗?如果我使用getContentType(),我会得到'text/html;字符集=iso-8859-15'