gpt4 book ai didi

java - 无法用java下载pdf

转载 作者:行者123 更新时间:2023-11-30 11:39:58 25 4
gpt4 key购买 nike

我正在尝试从 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");
}
}
}

你能帮帮我吗?

最佳答案

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

SQL error in file /data/htdocs/dokeos184/www/main/inc/tool_navigation_menu.inc.php at line 70

关于java - 无法用java下载pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12965015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com