How to Upload a File and How to Store it?

A quick and dirty Java Sockets API program:

// imports,*

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 = ) != -1 ) {
out.write(buf, 0, len);
out.flush(); // manually flush output stream for sockets

Paul-John A.

Quick Links:
Have Java Problem
Do you have a Java Question?

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Best Regards,
Java Programming Hints and Tips

All the site contents are Copyright © and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of or the content authors.