Hey, I have a project where, after serialising an array of objects, I have to send the file to another PC on the same network. I've googled "java networking" but some of the examples seem pretty complicated. What is the simplest way of achieving this? I have little/no networking experience beyond a basic understanding of IP addresses.
9 Answers
It depends on what you mean by "send a file". If the other PC has a shared drive that you can see over the network (e.g. in Windows explorer) then you can just copy it. FTP is another common option that would be pretty simple.
You could also look at using RMI to send the serialized data to another Java process.
Otherwise you might have to use the "complicated way". You'll probably find that it isn't as complicated as you might think you copy the examples and send the file as an array of bytes.
3 Comments
I would try sending the data via JMS messaging like ActiveMQ. This way the producer/consumer don't even need to be running at the same time.
Here is an example http://www.javablogging.com/simple-guide-to-java-message-service-jms-using-activemq/
Comments
Try having a look at Java RMI, especially the bit about sending serialized objects over the network.
1 Comment
Simple java code will work for moving files between computers over a network.
public class FileCopier {
public static void main(String args[]) throws Exception {
//give your files location anywhere in same network
File inboxDirectory = new File("data/inbox");
//give your output location anywhere in same network where you want to save/copy files
File outboxDirectory = new File("data/outbox");
outboxDirectory.mkdir();
File[] files = inboxDirectory.listFiles();
for (File source : files) {
if (source.isFile()) {
File dest = new File(
outboxDirectory.getPath()
+ File.separator
+ source.getName());
copyFile(source, dest);
}
}
}
private static void copyFile(File source, File dest)
throws IOException {
OutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[(int) source.length()];
FileInputStream in = new FileInputStream(source);
in.read(buffer);
try {
out.write(buffer);
} finally {
out.close();
in.close();
}
}
}
otherwise you can also use apache camel for accessing files in same network between computers
public class FileCopierWithCamel {
public static void main(String args[]) throws Exception {
CamelContext context = new DefaultCamelContext();
context.addRoutes(new RouteBuilder() {
public void configure() {
// from("file:data/inbox?noop=true").to("file:data/outbox");
from("file:data/inbox?noop=true").to("file:\\\\OthermachineName\\Output?autoCreate=true");
}
});
context.start();
// Thread.currentThread().join();
Thread.sleep(10000);
context.stop();
}
}
1 Comment
If you have a little experience with Spring and Maven i would go with Apache Camel, here is a example how to send a file via FTP from a java program to a FTP server (with little help from Spring), but Apache Camel understands a LOT protocols, for example plain file copy, send via mail, via message queue... I Really think just the transport via a carrier pigeon is missing in Apache Camel.