使用 Java 获取 Internet 文件并将其创建为 File 对象?

发布于 2024-12-08 09:26:30 字数 499 浏览 1 评论 0原文

可能的重复:
从 URLConnection 读取二进制文件

现在我有一个 URL:

(http://elsa.berkeley.edu/~saez/TabFig2005prel.xls 例如,使用 http: 方案)。

我需要访问此文件并将其将其存储为 File 对象在我的程序中,以便我可以完成以下工作。那么有人知道如何将其变成 File 对象吗?我不知道如何将输入流转换为文件。

谢谢 艾伦

Possible Duplicate:
Reading binary file from URLConnection

Now I have a URL:

(http://elsa.berkeley.edu/~saez/TabFig2005prel.xls for example, with http: scheme).

I need to access to this file and store it as a File object in my program so I can do my following work. So anyone knows how to make this to a File object? I have no idea how to covert a InputStream to a File.

Thank you
Allan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

十二 2024-12-15 09:26:30

样(http://commons.apache.org/io/

使用 apache commons-io怎么 有一个

FileUtils.copyURLToFile(URL,File);

How about using apache commons-io (http://commons.apache.org/io/)

It has a

FileUtils.copyURLToFile(URL,File);
浪漫之都 2024-12-15 09:26:30
int chunkSize = 500;

    BufferedInputStream in = new java.io.BufferedInputStream(new URL("http://javakata6425.appspot.com/db_imgs/projectsBinaries/photoLib/photoLib.jar").openStream());
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("d://temp//photoLib.jar"), chunkSize);
    byte[] data = new byte[chunkSize];
    int x = 0;
    while ((x = in.read(data, 0, chunkSize)) >= 0) {
        out.write(data, 0, x);
    }
    out.close();
    in.close();
int chunkSize = 500;

    BufferedInputStream in = new java.io.BufferedInputStream(new URL("http://javakata6425.appspot.com/db_imgs/projectsBinaries/photoLib/photoLib.jar").openStream());
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("d://temp//photoLib.jar"), chunkSize);
    byte[] data = new byte[chunkSize];
    int x = 0;
    while ((x = in.read(data, 0, chunkSize)) >= 0) {
        out.write(data, 0, x);
    }
    out.close();
    in.close();
花桑 2024-12-15 09:26:30

可以使用以下代码(假设InputStream对象名称为“content”)

File myFile = new File("newFile.ext");
FileOutputStream fos = null;
try {
    fos = new FileOutputStream(myFile);
    int read = 0;
    byte[] buff = new byte[1024];
    while ((read = content.read(buff)) > -1)
        fos.write(buff, 0, read);
} catch (IOException e1) {
    /* do something? */
} finally {
    if (fos != null) {
        try { fos.close() } catch (IOException e2) {/* do something? */}
    }
}

You can use the following code (assuming the InputStream object name is "content")

File myFile = new File("newFile.ext");
FileOutputStream fos = null;
try {
    fos = new FileOutputStream(myFile);
    int read = 0;
    byte[] buff = new byte[1024];
    while ((read = content.read(buff)) > -1)
        fos.write(buff, 0, read);
} catch (IOException e1) {
    /* do something? */
} finally {
    if (fos != null) {
        try { fos.close() } catch (IOException e2) {/* do something? */}
    }
}
黎歌 2024-12-15 09:26:30
URL url = new URL("http://elsa.berkeley.edu/~saez/TabFig2005prel.xls");
InputStream in = url.openStream();
try {
    OutputStream out = new FileOutputStream("output.xls");
    try {
        byte buf[] = new byte[4096];
        for (int n = in.read(buf); n > 0; n = in.read(buf)) {
            out.write(buf, 0, n);
        }
    } finally {
        out.close();
    }
} finally {
    in.close();
}
URL url = new URL("http://elsa.berkeley.edu/~saez/TabFig2005prel.xls");
InputStream in = url.openStream();
try {
    OutputStream out = new FileOutputStream("output.xls");
    try {
        byte buf[] = new byte[4096];
        for (int n = in.read(buf); n > 0; n = in.read(buf)) {
            out.write(buf, 0, n);
        }
    } finally {
        out.close();
    }
} finally {
    in.close();
}
惜醉颜 2024-12-15 09:26:30

伪代码:

create FileWriter pointing to "localdrive/local_copy.xls"
foreach byte you can read from the input stream:
    write the byte to the filewriter
flush the filewriter
close the filewriter

瞧。您现在已将文件保存在本地。

pseudocode:

create FileWriter pointing to "localdrive/local_copy.xls"
foreach byte you can read from the input stream:
    write the byte to the filewriter
flush the filewriter
close the filewriter

voila. You now have the file saved locally.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文