|
How to Upload a File and How to Store it? |
Views: 2031
|
Thread Tools | Rate Thread |
#1
|
|||
|
|||
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(); } |