【发布时间】:2015-08-17 10:07:38
【问题描述】:
感谢这个问题(link),我知道如何从互联网上下载文件。但是,我得到的不是 txt 文件中的普通文本,而是 html 响应。有谁知道我做错了什么?
这是我的代码:
// 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>
【问题讨论】:
-
可以这样试试吗? URL url = new URL("yourserver.com:80/filename"); // 读取文本 BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); String line; while ((line = in.readLine()) != null ) { System.out.println(line);// 将此行添加到文件输出流中 } in.close();
标签: java url httpconnection polarion