0

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 9

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Really?, RMI is the simplest? I'd say it's one of the more difficult ways.
@MeBigFatGuy: I didn't say it was the simplest! But RMI is as good way of communicating between two java processes on different machines, I use it all the time. For a one off task of sending a file it might be overkill.
the op asked for the simplest.
2

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

1

Try having a look at Java RMI, especially the bit about sending serialized objects over the network.

1 Comment

Really?, RMI is the simplest? I'd say it's one of the more difficult ways.
1

Follow the link below and you have a example of a file copy over TCP.

Link to example

Comments

1

Use Sockets, take a look at this example

Comments

0

You can simply create a shared folder for all of them and let them periodly check for new files.

Or you can write your own client server program so all clients listen on a specific port on which the server will send the file them.

Comments

0

I'd recommend using the RMI in KryoNet (which has little set-up compared with traditional RMI) with RMIIO.

Comments

0

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

Where to get the jar file of CamelContext?
-1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.