【问题标题】:Java: Download .txt File from URLJava:从 URL 下载 .txt 文件
【发布时间】:2016-10-24 14:32:12
【问题描述】:

我想从网站下载一个 .txt 文件,并且我的代码有效,所以我没有收到错误,它会加载文档,但文档中充满了 hmtl 代码,而不是我的内容。

public static void main(String[] args) {
    try {
        URL website = new URL("http://www.file-upload.net/download-11700212/document.txt.html");
        String filepath = "C://Users//" + System.getProperty("user.name") + "//Desktop//document.txt";

        ReadableByteChannel channel = Channels.newChannel(website.openStream());
        FileOutputStream stream = new FileOutputStream(filepath);

        stream.getChannel().transferFrom(channel, 0, Long.MAX_VALUE);

        System.out.println("Download successfull.");
    } catch (Exception e) {
        System.out.println("Download was not successfull.");
    }
}

下载本身可以,我在我的桌面上得到了txt文件,但是内容错误并且充满了html代码。

请帮忙。

谢谢。

【问题讨论】:

标签: java file url download web


【解决方案1】:

您尝试下载的 URL 是 HTML 页面,而不是文档本身。您应该尝试下载的页面上的链接是...

http://www.file-upload.net/download5.php?valid=451.69031370715&id=11700212&name=document.txt

但是,如果您希望确保下载的是文本文件,则应选择直接下载的文本文件,例如

http://humanstxt.org/humans.txt

【讨论】:

    【解决方案2】:

    我有一个名为 Python Webscraper 的 Python 项目,它可以读取 URL 并将其文本内容复制到没有 HTML 的文本文件中。

    您需要安装一个名为 Beautiful Soup 的包,然后从 GitHub 存储库运行代码。

    【讨论】:

    • 与其修正英语语法,不如试着找出答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-16
    • 2012-08-24
    • 1970-01-01
    • 2020-07-29
    相关资源
    最近更新 更多