从间接URL下载文件

时间:2013-03-15 11:04:32

标签: java download

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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 CAN`T正常工作

尝试那样