2Big2BeSmall
2Big2BeSmall

Reputation: 1378

Java URL download returns meta refresh tag instead of content

I have an URL of a file - for example:

Http://www.anyUrl.com/fileExample

which is offering to download this file when pasting it in Chrome/Explorer

I want to get this file using Java code.

Tried doing it using:

URL url = new URL("https://www.....");  InputStream......
or
DataInputStream ;
or
file = new File("https://www.....");

as well as this link.

None of them works for me.

All I got was something that looks like this:

</script><noscript><META http-equiv="refresh" content="0

which is not the content of the given file.

How can it be done in Java?

Upvotes: 0

Views: 390

Answers (1)

Mike Murphy
Mike Murphy

Reputation: 1046

I think the first thing you need to do is prove that the URL is actually providing a file. You can do this by entering the URL in a browser and see what comes back. Once your happy that the content of URL is a file you can use the examples here https://hc.apache.org/httpcomponents-client-ga/quickstart.html to download the file. See extracted example below.

Request.Get("http://targethost/homepage").execute().returnContent();

Upvotes: 3

Related Questions