如何在我的下载管理器java项目中下载视频?

时间:2013-04-06 15:01:11

标签: java video download download-manager

我正在使用java编写下载管理器,但此程序只下载pdf,jpeg,png,zip,无法下载视频.mp3,.flv或其他格式的视频。如何下载视频?这是我的Download.java的一部分。

 // Download file.
public void run() {

    InputStream stream = null;
    File file=new File(url);
    RandomAccessFile rfile=null;
     try {


        // Open connection to URL.
        HttpURLConnection connection =
                (HttpURLConnection) url.openConnection();

        // Specify what portion of file to download.
        connection.setRequestProperty("Range",
                "bytes=" + downloaded + "-");

        // Connect to server.
        connection.connect();


        int contentLength = connection.getContentLength();


  /* Set the size for this download if it
     hasn't been already set. */
        if (size == -1) {
            size = contentLength;

        }

        // Open file and seek to the end of it.
        rfile=file.openRandomAccessFile(url);
        rfile.seek(downloaded);

        stream = connection.getInputStream();

        while (status == DOWNLOADING) {
    /* Size buffer according to how much of the
       file is left to download. */
            byte buffer[];
            if (size - downloaded > MAX_BUFFER_SIZE) {
                buffer = new byte[MAX_BUFFER_SIZE];
            } else {
                buffer = new byte[size - downloaded];
            }

            // Read from server into buffer.
            int read = stream.read(buffer);
            if (read == -1){
                System.out.println(file.getFileName()+" was downloaded");
                break;
            }

            // Write buffer to file.
            rfile.write(buffer, 0, read);
            downloaded += read;

        }

  /* Change status to complete if this point was
     reached because downloading has finished. */
        if (status == DOWNLOADING) {
            status = COMPLETE;

        }
    } catch (Exception e) {
       System.out.println("Error!");
    } finally {
        // Close file.
        if (rfile != null) {
            try {
                rfile.close();
            } catch (Exception e) {}
        }

        // Close connection to server.
        if (stream != null) {
            try {
                stream.close();
            } catch (Exception e) {}
        }
    }

0 个答案:

没有答案