java - 使用 RandomAccessFile 通过 Internet 下载文件

标签 java download random-access

我在互联网上浏览随机 Java 代码,发现了 download manager 的源代码。它使用 RandomAccessFile 下载文件。但我不知道的一件事是它会下载到哪里。下载文件的方法如下:

public void startDownload() {
    System.out.println("Starting...");
    RandomAccessFile file = null;
    InputStream stream = null;

    try {
        URL downloadLink = new URL("http://www.website.com/file.txt");

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

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

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

        // Make sure the code is in the 200 range
        if (connection.getResponseCode() / 100 != 2) {
            error();
        }

        // Check for valid content length
        int contentLength = connection.getContentLength();
        if (contentLength < 1) {
            error();
        }

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

        // Open file and seek to the end of it
        file = new RandomAccessFile(getFileName(downloadLink), "rw");  
              // getFileName returns the name of the file mentioned in the URL

        file.seek(downloaded);

        stream = connection.getInputStream();

        while (status == DOWNLOADING) {
            System.out.println("Progress: " + getProgress() + "%");

            // Size the 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 the server into the buffer
            int read = stream.read(buffer);
            if (read == -1) {
                break;
            }

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

        if (status == DOWNLOADING) {
            status = COMPLETE;
            stateChanged();
        }


    } catch (Exception e) {
        error();
    } finally {
        // Close the stream and RAF
    }
    System.out.println("Done!");
}

如果这是显而易见的,我很抱歉。我是 RandomAccessFile 类的新手,因为我今天刚刚了解到它。

最佳答案

它将把它下载到当前工作目录(即运行 java 命令的位置),文件名将由 getFileName(downloadLink) 给出。

关于java - 使用 RandomAccessFile 通过 Internet 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12030768/

相关文章:

java - 在 noSQL "counting QL"数据存储上运行大量 "GAE"

java - 由 SimpleDateFormat.parse() 分配的空日期

Java 泛型 : Question about methods with generic return type (but no generic parameter)

node.js - nodejs从url下载并解压文件,错误No END header found

java - RandomAccessFile 打印到文本文件的麻烦

java - JavaScript 和 Java 之间正则表达式模式的差异?

c# - 如何更改 .NET WebClient 对象的超时

algorithm - 以 O(1) 访问任意元素的随机递增序列?

java - 调用随机函数

java - 两台电脑之间如何发送文件