PDA

View Full Version : Java: Sending a file to another computer


keatonvictor
2009-01-15, 08:29 AM CST
Hi Everyone

I am pretty new to Java network programming so I need your humble opinions and advice.

If I have 2 computers, one of which is on but does not have anyone touching it. What would be the best way to remotely transfer files to this remote machine. This would be done using java. Its simply a case of. I run my java file and it sends the file

/home/user/myfile.txt ( local machine, with user)

to

/home/user/myfile.txt ( remote machine, no user, however on!)

All advise is greatly appreciated. Thats everyone.

RupertPupkin
2009-01-15, 03:17 PM CST
You could use sockets, but then you'd need to have a daemon running on the remote machine listening on a certain port. You'd have to write that remote server daemon that receives the files, in addition to the local client program that sends the files. That may or may not be a problem for you. It's not that hard to do.

An easier way is to use plain old ftp, since the remote machine probably has an ftp server already installed. Sun's JDK has an undocumented ftp class library (do a net search on "sun.net.ftp"). You could use that like this (sunftp.java):
import sun.net.ftp.*;
import java.io.*;

public class sunftp {
public static void main(String args[]) {
String hostname = "some.remote.machine"; //Remote FTP server: Change this
String username = "user"; //Remote user name: Change this
String password = "start123"; //Remote user password: Change this
String upfile = args[0]; //File to upload passed on command line
String remdir = "/home/user"; //Remote directory for file upload
FtpClient ftp = new FtpClient();
try {
ftp.openServer(hostname); //Connect to FTP server
ftp.login(username, password); //Login
ftp.binary(); //Set to binary mode transfer
ftp.cd(remdir); //Change to remote directory
File file = new File(upfile);
OutputStream out = ftp.put(file.getName()); //Start upload
InputStream in = new FileInputStream(file);
byte c[] = new byte[4096];
int read = 0;
while ((read = in.read(c)) != -1 ) {
out.write(c, 0, read);
} //Upload finished
in.close();
out.close();
ftp.closeServer(); //Close connection
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
You'll get some warning messages saying "sun.net.ftp.FtpClient is Sun proprietary API and may be removed in a future release", but the program will work. Run it like this:
java sunftp /home/user/myfile.txt

An alternative is to use the Jakarta Commons/Net libraries (http://commons.apache.org/net/), which has an ftp library.

Absurd
2009-01-15, 03:42 PM CST
Hi Everyone

I am pretty new to Java network programming so I need your humble opinions and advice.

If I have 2 computers, one of which is on but does not have anyone touching it. What would be the best way to remotely transfer files to this remote machine.

client: java.net.HttpURLConnection, server: One of lightweight java http servers which can be embedded as backend in mature java application like Jetty