1

Thanks to this question( link ), I know how to download a file from the internet. However, instead of a normal Text in a txt-file i get a html response. Does anyone know what I'am doing wrong?

Here is my code:

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

Response:

<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>
Community
  • 1
  • 1
prosk
  • 133
  • 1
  • 1
  • 9
  • Can you try in this way? URL url = new URL("http://yourserver.com:80/filename"); // read text BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); String line; while ((line = in.readLine()) != null) { System.out.println(line);// Add this line into file output stream } in.close(); – Ravindra babu Aug 17 '15 at 10:17

2 Answers2

0

Since comment is not readable, posting the answer.

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();
    }
}

Below code worked for to read a text file which is in below format

Novak Djokovic
Andy Murray
Roger Federer
Nishokiri

Output

Novak Djokovic

Andy Murray

Roger Federer

Nishokiri

Community
  • 1
  • 1
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
-1

It looks like it might me searching for the class "revision=1260" on the webpage and returning that

DieVeenman
  • 457
  • 1
  • 3
  • 18