Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Thursday, February 28, 2008

Sample Code On How to Upload a File and How to Store it?

A quick and dirty Java Sockets API program:

// imports java.net.Socket, java.io.*

void uploadFile(String hostAddr, int port, String fileName) throws
FileNotFoundException, IOException {
byte[] buf = new byte[1000];
int len = -1;

// connect to host at hostAddr, port
Socket sock = new Socket(hostAddr, port);

FileInputStream in = new FileInputStream(fileName);
OutputStream out = sock.getOutputStream();

while ( ( len = in.read(buf) ) != -1 ) {
out.write(buf, 0, len);
out.flush(); // manually flush output stream for sockets
}
out.close();
sock.close();
in.close();
}

No comments:

Post a Comment