我正在开发一个从给定 URL 下载文件的 Java 程序。它非常适合直接链接,例如:
http://cdn.muvee.com/downloads/muveeRevealX_10.5.0.23245_2760.exe?AWSAccessKeyId=1026JPYBE0QTTK67WVG2&Expires=1363299511&Signature=ROKk0Jav1Jw5HGU4fcBs6ADhPqI%3D&Extension=.exe
但不适用于间接链接,例如:
http://www.youtube.com/watch?v=8BvmqPymyfY&list=PL6EE0CD02910E57B8
Youtube 链接程序的输出是:-
Fri Mar 15 16:30:48 IST 2013
Content-Type = text/html; charset=utf-8
Content-Disposition = null
Content-Length = -1
fileName = watch?v=8BvmqPymyfY&list=PL6EE0CD02910E57B8
java.io.FileNotFoundException: E:\watch?v=8BvmqPymyfY&list=PL6EE0CD02910E57B8 (The filename, directory name, or volume label syntax is incorrect)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:212)
at java.io.FileOutputStream.<init>(FileOutputStream.java:104)
at HttpDownloadUtility.downloadFile(HttpDownloadUtility.java:62)
at HttpDownloader.main(HttpDownloader.java:14)
HttpDownloadUtility.java 的代码:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpDownloadUtility {
private static final int BUFFER_SIZE = 4096;
public static void downloadFile(String fileURL, String saveDir)
throws IOException {
URL url = new URL(fileURL);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
int responseCode = httpConn.getResponseCode();
httpConn.connect();
// 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();
}
}
主文件:HttpDownload.java:
import java.io.IOException;
import java.util.Date;
public class HttpDownloader {
public static void main(String[] args) {
String fileURL = "http://www.youtube.com/watch?v=8BvmqPymyfY&list=PL6EE0CD02910E57B8";
String saveDir = "E:/";
try {
System.out.println(new Date().toString());
HttpDownloadUtility.downloadFile(fileURL, saveDir);
System.out.println(new Date().toString());
} catch (IOException ex) {
ex.printStackTrace();
}
}
}