簡體   English   中英

Java:從URL下載.txt文件

[英]Java: Download .txt File from URL

我想從網站上下載一個.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代碼。

請幫忙。

謝謝。

您嘗試從中下載的URL是HTML頁面,而不是文檔本身。 您應嘗試從該頁面下載的鏈接是...

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

但是,如果您希望保證下載的是文本文件,則應選擇一個文本文件直接下載,例如

http://humanstxt.org/humans.txt

我有一個名為Python Webscraper的Python項目,該項目可以讀取URL並將其文本內容復制到沒有HTML的文本文件中。

您需要安裝一個名為Beautiful Soup的軟件包,然后從GitHub存儲庫中運行代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM