Urgent: File download using HTTP

Hello,

I have some files stored on a Remote machine A. The machine A accepts HTTP connection only.

I want to write a java program which runs on Machine B, creates a connection to machine A and gets/downloads the file and store it on machine B. The file is a 3gp/mp4 file.

Can you please advise/suggest as how this can be done?

Regards.

[366 byte] By [singalga] at [2007-10-3 8:10:10]
# 1

You can use java.net.HttpURLConnection to download a file using HTTP protocol. E.g. there is a file on machineA at /some/location/abc.mp4 - you can download that file using:

HttpURLConnection con = new URL("http://machineA/some/location/abc.mp4").openConnection();

con.connect();

InputStream is = con.getInputStream();

// now, read the content and store it on the local machine - machineB

I hope this serves your purpose.

- Naga Ranjan Darbha

nrdarbhaa at 2007-7-15 3:14:31 > top of Java-index,Core,Core APIs...
# 2

Thanks for your reply.

I am getting the following error now, can some one please advise how to fix this:

HttpTest.java:12: incompatible types

found: java.net.URLConnection

required: java.net.HttpURLConnection

(The openConnection method returns a URLConnection object and not a HttpURLConnection object)

I also tried the below code:

URL url = new URL("http","10.176.96.63",9000,"c:\\database.properties");

InputStream is = url.openStream();

This code gives an exception:

java.net.SocketException: Unexpected end of file from server

Can someone please help...

Thanks.

singalga at 2007-7-15 3:14:31 > top of Java-index,Core,Core APIs...
# 3

The error is telling you that in your file HttpTest.java at line 12, that you are trying to use the wrong class.

URL url = new URL("http://10.176.96.63:9000/database.properties");

Obviously, your local Oracle database server at 10.176.96.63 has a properties file that you want to get.

Do you even know what port 9000 on an Oracle server is for?

watertownjordana at 2007-7-15 3:14:31 > top of Java-index,Core,Core APIs...
# 4

Thanks for your reply.

Sorry if the file name "database.properties" has confused you. It has nothing to with the oracle database and there is no database installed on this machine. "database.properties" is only a dummy file name and file used for my testing. once I am able to get this file, i will extend the code to exact file name/location, which will have a 3gp/mp4 extension.

Regarding port 9000, all I know is that there is a machine A which accepts only "http" connections and on port 9000 only.

So I have to use the same port to get the file using HTTP only.

Please Help!

Regards,

singalga at 2007-7-15 3:14:31 > top of Java-index,Core,Core APIs...
# 5

I've got it to work now, using the below:

URL url = new URL("http","10.176.96.63",9000,"/html/verify.3gp");

con = (HttpURLConnection)url.openConnection();

con.connect();

is = con.getInputStream();

bufIn = new BufferedInputStream(is);

for (;;)

{

int data = bufIn.read();

if (data == -1)

break;

else

System.out.print ( (char) data);

}

Thanks all for your help!

Regards.

singalga at 2007-7-15 3:14:31 > top of Java-index,Core,Core APIs...