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

|| 嘿, 我有一个项目,在其中序列化对象数组之后,必须将文件发送到同一网络上的另一台PC。我已经用Google搜索了“ java网络”,但是其中一些示例似乎非常复杂。最简单的方法是什么?除了对IP地址的基本了解之外,我几乎没有网络经验。
已邀请:
如果您对Spring和Maven有一点经验,我会使用Apache Camel,这是一个示例,该示例介绍了如何通过FTP从Java程序向FTP服务器发送文件(在Spring的帮助下),但是Apache Camel可以理解很多协议,例如纯文件复制,通过邮件发送,通过消息队列发送...我真的认为Apache Camel仅缺少通过信鸽的运输。
这取决于您“发送文件”的含义。如果另一台PC具有共享驱动器,您可以在网络上查看它(例如在Windows资源管理器中),则只需复制它即可。 FTP是另一个非常简单的通用选项。 您还可以考虑使用RMI将序列化的数据发送到另一个Java进程。 否则,您可能必须使用“复杂方式”。您可能会发现它并不像您认为的复制示例并将文件作为字节数组发送那样复杂。
尝试看看Java RMI,尤其是有关通过网络发送序列化对象的知识。
我会尝试通过像ActiveMQ这样的JMS消息发送数据。这样,生产者/消费者甚至不需要同时运行。 这是一个示例http://www.javablogging.com/simple-guide-to-java-message-service-jms-using-activemq/
单击下面的链接,您将看到一个通过TCP复制文件的示例。 链接到示例
使用套接字,看看这个例子
您可以简单地为所有文件创建一个共享文件夹,并让它们定期检查新文件。 或者,您可以编写自己的客户端服务器程序,以便所有客户端都在服务器将向其发送文件的特定端口上进行侦听。
我建议在带有RMIIO的KryoNet中使用RMI(与传统RMI相比设置很少)。
简单的Java代码可用于通过网络在计算机之间移动文件。 公共类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();
    }
}
} 否则,您也可以使用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();
}
}

要回复问题请先登录注册