Go Back   Wiki NewForum | Latest Entertainment News > Career Forum & Tips > Tech Forum & Tutorial > Java Forum & Tutorial


How to Upload a File and How to Store it?


Reply
Views: 2031  
Thread Tools Rate Thread
  #1  
Old 05-25-2009, 04:50 PM
bholus7
Guest
 
Posts: n/a
Default How to Upload a File and How to Store it?

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();
}

Reply With Quote
Reply

New topics in Java Forum & Tutorial





Powered by vBulletin® Version 3.8.10
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
WikiNewForum)