从URL获取文件 - 错误

时间:2015-08-17 10:07:39

标签: java url httpconnection polarion

感谢这个问题(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);

EDITED

响应:

<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>

2 个答案:

答案 0 :(得分:0)

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

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

输出

  诺瓦克·德约科维奇

     

Andy Murray

     

罗杰·费德勒

     

Nishokiri

答案 1 :(得分:-1)

看起来我可能正在寻找班级&#34; revision = 1260&#34;在网页上并返回