gpt4 book ai didi

java - 从 URL 获取文件 - 错误

转载 作者:行者123 更新时间:2023-12-01 06:12:36 27 4
gpt4 key购买 nike

感谢这个问题( link ),我知道如何从互联网下载文件。然而,我得到的是 html 响应,而不是 txt 文件中的普通文本。有谁知道我做错了什么?

这是我的代码:

                    // Install Authenticator
MyAuthenticator.setPasswordAuthentication("Username", "Password");
Authenticator.setDefault (new MyAuthenticator(Main.getPropertyPath()));

URL website = new URL("http://.../5-Anhang.txt?revision=1260");
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream("information.txt");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);

已编辑

回应:

<html>
<body onLoad='document.forms["login"].submit();'>
<form id='login' method='POST' action='/polarion/j_security_check'>
<input type='hidden' name='j_username' value='null'/>
<input type='hidden' name='j_password' value='null'/>
<noscript>
<input type='submit' value="Login"/>
</noscript>
</form>
</body>
</html>

最佳答案

由于评论不可读,因此发布答案。

import java.io.*;
import java.net.*;

public class App {
public static void main(String[] args) throws Exception{

URL url = new URL("http://localhost:8080/js/txt1.txt");
// read text
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
}
}

下面的代码用于读取以下格式的文本文件

Novak Djokovic
Andy Murray
Roger Federer
Nishokiri

输出

Novak Djokovic

Andy Murray

Roger Federer

Nishokiri

关于java - 从 URL 获取文件 - 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32047694/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com