我正在编写一个Java应用程序,应在此处下载Pdf表单:https://forms.business.gov.au/smartforms/servlet/SmartForm.pdf?formCode=RRDAA
我尝试了各种选择,包括:
问题是,当我使用这些示例时,正在下载的内容仅仅是打开URL时看到的网站内容。
但是我想下载实际的Pdf文件,而不是该网站。
感谢您的任何帮助。
尝试这个。它对我有用。
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
/**
* @author ucguy4u
*
*/
public class DownloadPDF {
public static void main(String[] args) {
URL url;
try {
url = new
URL("http://www.africau.edu/images/default/sample.pdf");
BufferedInputStream bufferedInputStream = null;
bufferedInputStream = new BufferedInputStream(url.openStream());
FileOutputStream stream;
stream = new FileOutputStream("C:\\simple.pdf");
int count=0;
byte[] b1 = new byte[100];
while((count = bufferedInputStream.read(b1)) != -1) {
System.out.println("b1:"+b1+">>"+count+ ">> KB downloaded:"+new File("C:\\simple.pdf").length()/1024);
stream.write(b1, 0, count);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
P.S .:
请确保您不在专用网络中工作。如果您在专用网络中工作,则必须在代理下运行它,这将需要对代理进行其他处理。
是通用的,我发现这段代码很有用。它将下载您通过url传递到方法的任何文件。
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* A utility that downloads a file from a URL.
* @author ucguy4u
*
*/
public class HttpDownloadUtility {
private static final int BUFFER_SIZE = 4096;
public static void main(String args[]){
try {
downloadFile("http://www.africau.edu/images/default/sample.pdf","C:\\");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Downloads a file from a URL
* @param fileURL HTTP URL of the file to be downloaded
* @param saveDir path of the directory to save the file
* @throws IOException
*/
public static void downloadFile(String fileURL, String saveDir)
throws IOException {
URL url = new URL(fileURL);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
int responseCode = httpConn.getResponseCode();
// always check HTTP response code first
if (responseCode == HttpURLConnection.HTTP_OK) {
String fileName = "";
String disposition = httpConn.getHeaderField("Content-Disposition");
String contentType = httpConn.getContentType();
int contentLength = httpConn.getContentLength();
if (disposition != null) {
// extracts file name from header field
int index = disposition.indexOf("filename=");
if (index > 0) {
fileName = disposition.substring(index + 10,
disposition.length() - 1);
}
} else {
// extracts file name from URL
fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1,
fileURL.length());
}
System.out.println("Content-Type = " + contentType);
System.out.println("Content-Disposition = " + disposition);
System.out.println("Content-Length = " + contentLength);
System.out.println("fileName = " + fileName);
// opens input stream from the HTTP connection
InputStream inputStream = httpConn.getInputStream();
String saveFilePath = saveDir + File.separator + fileName;
// opens an output stream to save into file
FileOutputStream outputStream = new FileOutputStream(saveFilePath);
int bytesRead = -1;
byte[] buffer = new byte[BUFFER_SIZE];
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.close();
inputStream.close();
System.out.println("File downloaded");
} else {
System.out.println("No file to download. Server replied HTTP code: " + responseCode);
}
httpConn.disconnect();
}
}