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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java novice needs a little help 1

Status
Not open for further replies.

IDTstudios1

Programmer
Aug 8, 2004
72
US
I'm primarily a PHP and programmer, however a recent need to pull off a certain function has tossed me into the Java world. I found the snippet of code below on another thread. It does exactly what I need, I just have no clue on how to compile it.

I know enough to have download the Java SDK with NetBeans and have been trying to figure it out for several hours with no luck. I could always go buy a book or take a class but considering that this will probably be the only time I ever need to compile a java app it seemed best to just ask for help.

If someone could give me a step by step on how to make the code below usable in a compiled form I would greatly appreciate it.


Code:
File f = new File("somefile.txt");
FileInputStream fis = new FileInputStream(f);
byte[] data = new byte[(int)f.length()];
fis.read(data);
fis.close();

String u = "[URL unfurl="true"]http://sync.foxysync.com/test.php?action=process&id=1";[/URL]
URL url = new URL(u);
HttpURLConnection huc = (HttpURLConnection)url.openConnection();
huc.setRequestMethod("POST");
huc.setDoOutput(true);
huc.setUseCaches(false);
huc.setFollowRedirects(true);

DataOutputStream dos = new DataOutputStream(huc.getOutputStream());
dos.write(data, 0, data.length);
dos.flush();
dos.close();

huc.disconnect();

Thanks,
Andrew
 
You don't need netbeans to compile Java - if you are a noob, then I would avoid using an IDE until you understand what is going on.

Save the code as Test.java.
Open a console/terminal window.

cd to where you saved it.

Then compile it like this :

javac Test.java

And run it like this :

java -cp . Test

If you can't get that to work, then read this tutorial :

Code:
import java.io.*;
import java.net.*;

public class Test {
	public static void main(String args[]) throws Exception {
		File f = new File("somefile.txt");
		FileInputStream fis = new FileInputStream(f);
		byte[] data = new byte[(int)f.length()];
		fis.read(data);
		fis.close();

		String u = "[URL unfurl="true"]http://sync.foxysync.com/test.php?action=process&id=1";[/URL]
		URL url = new URL(u);
		HttpURLConnection huc = (HttpURLConnection)url.openConnection();
		huc.setRequestMethod("POST");
		huc.setDoOutput(true);
		huc.setUseCaches(false);
		huc.setFollowRedirects(true);

		DataOutputStream dos = new DataOutputStream(huc.getOutputStream());
		dos.write(data, 0, data.length);
		dos.flush();
		dos.close();

		huc.disconnect();
	}
}


--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top