简体   繁体   English

使用Java通过网络在计算机之间发送文件

[英]Sending files between computers over a network using java

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. 嘿,我有一个项目,在其中序列化对象数组之后,必须将文件发送到同一网络上的另一台PC。 I've googled "java networking" but some of the examples seem pretty complicated. 我已经在“ java网络”上进行了搜索,但是其中一些示例似乎非常复杂。 What is the simplest way of achieving this? 最简单的方法是什么? I have little/no networking experience beyond a basic understanding of IP addresses. 除了对IP地址的基本了解之外,我几乎没有网络经验。

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 (eg in Windows explorer) then you can just copy it. 如果另一台PC具有共享驱动器,您可以通过网络(例如,在Windows资源管理器中)查看该共享驱动器,则只需复制它即可。 FTP is another common option that would be pretty simple. FTP是另一个非常简单的通用选项。

You could also look at using RMI to send the serialized data to another Java process. 您还可以考虑使用RMI将序列化的数据发送到另一个Java进程。

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. 您可能会发现它并不像您认为的复制示例并将文件作为字节数组发送那样复杂。

尝试看看Java RMI ,特别是有关通过网络发送序列化对象的知识。

I would try sending the data via JMS messaging like ActiveMQ . 我会尝试通过像ActiveMQ这样的JMS消息发送数据。 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/ 这是一个示例http://www.javablogging.com/simple-guide-to-java-message-service-jms-using-activemq/

Follow the link below and you have a example of a file copy over TCP. 单击下面的链接,您将看到一个通过TCP复制文件的示例。

Link to example 链接到示例

使用套接字,看看这个例子

Simple java code will work for moving files between computers over a network. 简单的Java代码可用于通过网络在计算机之间移动文件。

public class FileCopier { 公共类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 否则,您也可以使用apache骆驼在计算机之间访问同一网络中的文件

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();
}

} }

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. 或者,您可以编写自己的客户端服务器程序,以便所有客户端都在服务器将向其发送文件的特定端口上进行侦听。

我建议在KryoNet中使用RMI(与传统RMI相比设置很少)与RMIIO一起使用

如果您对Spring和Maven有一点经验,我会使用Apache Camel这是一个示例该示例说明了如何通过FTP从Java程序向FTP服务器发送文件(在Spring的帮助下),但是Apache Camel可以理解很多协议,例如纯文件复制,通过邮件发送,通过消息队列发送...我真的认为Apache Camel仅缺少通过信鸽的运输。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM