-8

我正在开发一个从给定 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();
        }
    }
}
4

2 回答 2

0

您不能简单地下载 youtube 中的文件。当您查看您使用的 URL 时,您可以看到它所使用的 url 以文件扩展名结尾,即它们是直接链接,该 url 直接指向文件。如果您必须从间接链接(例如 youtube)下载它,您必须先获取内容,然后才能保存它。据我所知,在 youtube 中,Flash 播放器直接获取视频流。

于 2013-03-15T11:10:24.083 回答
0

我遇到了同样的问题,所以问题是,您需要将扩展​​名添加到视频中,因为:

downloadFile(String fileURL, String saveDir)

我将其覆盖为

downloadFile(String fileURL, String saveDir, String name)

然后在

// extracts file name from header field
                ......
                int index = disposition.indexOf("filename=");
                if (index > 0) {
                    fileName = disposition.substring(index + 10,
                            disposition.length() - 1);

替换为

String fileName =name+".mp4";

但它仅适用于特定服务器,我已经从 .com 服务器下载,但 youtube.com 或 *.in 无法正常工作

试试这样

于 2015-10-05T15:49:51.697 回答