Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

download file with http

Status
Not open for further replies.

stemy

Programmer
Aug 25, 2001
39
IL
Hi
I need to write a small client application in java that uses http in order to downlaod a file from the internet.
I suppose I should use
Code:
URLConnection.getInputStream[code] and send that stream as a constructor parameter for another cusom input stream, only I don't know which one to use. Am I getting this right? what should I do?
thanx.
 
This code will connect to a URL, and download ascii data. If you need to download byte[] data, then you need to change the input/output streams slightly, but this will give you an idea how to start with a simple ascii file.

Code:
	URL url = new URL("[URL unfurl="true"]http://www.acme.com/files/downloads/afile.html");[/URL]
	HttpURLConnection httpURL = (HttpURLConnection)url.openConnection();
	httpURL.setRequestMethod("GET");
	httpURL.setDoInput(true);
	httpURL.setDoOutput(true);
	httpURL.setUseCaches(false);

	// This commented block is used for writing parameters to the URL
	// EG - if used the final URL would look like [URL unfurl="true"]http://www.acme.com/files/downloads/afile.html?user=abc&area=something[/URL]
	//String params = "user=abc&area=something";
	//System.out.println(params);
	//byte[] data = params.getBytes();
	//DataOutputStream dos = new DataOutputStream(httpURL.getOutputStream() );
	//dos.write(data);
	//dos.close();

	BufferedReader br = new BufferedReader(new InputStreamReader(httpURL.getInputStream()));
	PrintWriter out = new PrintWriter(new OuputStreamWriter(new FileOutputStream("afile.html")));
	
	String line = "";
	while ((line = br.readLine()) != null) {
		out.println(line);
		System.out.println(line);
	}
 
PS, don't forget to flush and close your streams at the end -

out.flush();
out.close();
br.close();
 
thanx. but now, how do I upload a file ?
 
plus, I can't seen to make it work with binary files.
 
When you say "upload" - do you mean from a browser client to the server ? The code sample I posted will do it for ascii files, but as I said, requires some modification for binary files.
 
I need, from within an application that doesn't have to do with a browser, to specify a file and URL, and to upload that file.
I whould also appriciate help with those binary files.
thanks a lot.
 
I'm confused. Your title for this post says "download", but now you say "upload". Do you wish to transfer a file from a web server to an application, or from an application to a web server ?
 
The code I posted will get a file from a web server to the application. To used binary files, replace the BufferedReader object with a BufferedInStream, and the PrintWriter object with BufferedOutputStream, and then read the data an int at a time - this will succesfully download a binary file.

To upload a file from an application to a web server, the process is similar, but reversed, and you of course also need some code to read the file in at the web server end.
 
I tried to look it up but I got nothing. what exactly should I put on the server? should it be a jsp file? a class file? what code should I run on the sever?
 
Well, one way would be to have a Servlet that implements the doPost() (HTTP POST) method. You can attach the binary file to the request parameter, and then write that out to disk, or db or whatever.

A common way to do this with a browser is to use the <input type=&quot;file> tag, but I am not sure how to implement this using a now-browser call. You'll have to experiment !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top