如何使用Java通过网络将文件从一台计算机传输到另一台计算机?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1109307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:52:51  来源:igfitidea点击:

How to transfer files from one computer to another over the network using Java?

javafilenetworking

提问by

I need a simple application, preferably a cross-platform one, that enables sending of files between two computers.

我需要一个简单的应用程序,最好是跨平台的应用程序,它可以在两台计算机之间发送文件。

It just need to accept and send the files, and show a progress bar. What applications could I use or how could I write one?

它只需要接受和发送文件,并显示一个进度条。我可以使用哪些应用程序或如何编写一个应用程序?

采纳答案by jjnguy

Sending and Receiving Files

发送和接收文件

The sending and receiving of a file basically breaks down to two simple pieces of code.

文件的发送和接收基本上分解为两段简单的代码。

Recieving code:

接收代码:

ServerSocket serverSoc = new ServerSocket(LISTENING_PORT);

Socket connection = serverSoc.accept();

// code to read from connection.getInputStream();

Sending code:

发送代码:

File fileToSend;
InputStream fileStream = new BufferedInputStream(fileToSend);

Socket connection = new Socket(CONNECTION_ADDRESS, LISTENING_PORT);
OutputStream out = connection.getOutputStream();

// my method to move data from the file inputstream to the output stream of the socket
copyStream(fileStream, out);

The sending piece of code will be ran on the computer that is sending the code when they want to send a file.

当他们想要发送文件时,发送代码将在发送代码的计算机上运行。

The receiving code needs to be put inside a loop, so that everytime someone wants to connect to the server, the server can handle the request and then go back to waiting on serverSoc.accept().

接收代码需要放在一个循环中,这样每次有人想要连接到服务器时,服务器可以处理请求,然后回到等待 serverSoc.accept()。

To allow sending files between both computers, each computer will need to run the server (receiving code) to listen for incoming files, and they will both need to run the sending code when they want to send a file.

为了允许在两台计算机之间发送文件,每台计算机都需要运行服务器(接收代码)来侦听传入的文件,并且它们都需要在要发送文件时运行发送代码。

Progress Bar

进度条

The JProgressBarin Swing is easy enough to use. However, getting it to work properly and show current progress of the file transfer is slightly more difficult.

JProgressBarSwing中是很容易的使用。但是,让它正常工作并显示文件传输的当前进度稍微困难一些。

To get a progress bar to show up on a form only involves dropping it onto a JFrameand perhaps setting setIndeterminate(false)so hat it shows that your program is working.

要让进度条显示在表单上,​​只需将其拖放到一个JFrame并且可能设置setIndeterminate(false)它显示您的程序正在运行。

To implement a progress bar correctly you will need to create your own implementation of a SwingWorker. The Java tutorials have a good example of this in theirlesson in concurrency.

要正确实现进度条,您需要创建自己的SwingWorker. Java 教程在他们的并发课程中有一个很好的例子。

This is a fairly difficult issue on its's own though. I would recommend asking this in it's own question if you need more help with it.

不过,这本身就是一个相当困难的问题。如果您需要更多帮助,我建议您在自己的问题中提出这个问题。

回答by AlbertoPL

You can write one by using Socket programming in Java. You would need to write a Server and a Client program. The server would use a ServerSocket to listen for connections, and the Client would use a Socket to connect to that server on the specified port.

您可以使用 Java 中的 Socket 编程来编写一个。您需要编写一个服务器和一个客户端程序。服务器将使用 ServerSocket 来侦听连接,而客户端将使用 Socket 在指定端口上连接到该服务器。

Here's a tutorial: http://www.javaworld.com/jw-12-1996/jw-12-sockets.html

这是一个教程:http: //www.javaworld.com/jw-12-1996/jw-12-sockets.html

回答by Alistair Evans

Check out thistutorial, it's a really basic example. You would probably also want to send control headers prior to the actual file being sent, containing the size of the file, filename, etc.

查看教程,这是一个非常基本的示例。您可能还希望在发送实际文件之前发送控制标头,其中包含文件的大小、文件名等。

Alternatively, base it on an existing protocol, like thisproject.

或者,基于现有协议,如项目。

回答by Matthew Flaschen

I would strongly consider using FTP. Apache has a FTP clientand a server

我会强烈考虑使用 FTP。Apache 有一个 FTP客户端和一个服务器

Edit: spdenne's suggestion of HTTP is also good, especially if everyone has Java 6. If not, you can use something like Tiny Java Web Server.

编辑:spdenne 对 HTTP 的建议也很好,特别是如果每​​个人都有 Java 6。如果没有,你可以使用类似Tiny Java Web Server 的东西。

回答by Benoit Courtine

Can you install FTP servers on (one of) your machines ?

您可以在(其中一台)机器上安装 FTP 服务器吗?

If you can, you will just have to use a FTP client (FileZilla for example, which have a progress bar).

如果可以,您只需要使用 FTP 客户端(例如 FileZilla,它有一个进度条)。

回答by Stephen Denne

Sun's Java 6 includes a light-weight HTTP server APIand implementation. You could fairly easily use this to serve your file, using URLConnection to obtain it.

Sun 的 Java 6 包括一个轻量级的HTTP 服务器 API和实现。你可以很容易地使用它来提供你的文件,使用 URLConnection 来获取它。

回答by Nate

Woof is a cool Python script that might work for you:

Woof 是一个很酷的 Python 脚本,可能对你有用:

http://www.home.unix-ag.org/simon/woof.html

http://www.home.unix-ag.org/simon/woof.html

回答by Tim H

Two popular apps are "scp" and "rsync". These are standard on Linux, are generally available on Unix and can be run on Windows under cygwin, although you may be able to find windows-native apps that can do it as well. (PuTTY can serve as an SCP client).

两个流行的应用程序是“scp”和“rsync”。这些在 Linux 上是标准的,在 Unix 上通常可用,并且可以在 cygwin 下在 Windows 上运行,尽管您可能能够找到也可以这样做的 Windows 原生应用程序。(PuTTY 可以作为 SCP 客户端)。

For any sort of pc-to-pc file transfer, you need to have a listener on the destination PC. This can be a daemon app (or Windows system process), or it can be a Unix-style "superserver" that's configured to load and run the actual file-copy app when someone contacts the listening port.

对于任何类型的 pc 到 pc 文件传输,您需要在目标 PC 上有一个侦听器。这可以是守护程序应用程序(或 Windows 系统进程),也可以是 Unix 风格的“超级服务器”,配置为在有人联系侦听端口时加载和运行实际的文件复制应用程序。

SCP and one of the rsync modes do require that there be some sort of remote login capability. Rsync can also publish resources that it will handle directory. Since the concept of a Windows "remote login" isn't as well-established as it is under Linux, this may be preferable. Plus it limits remote access to defined sources/targets on the destination machine instead of allowing access to any (authorized) part of the filesystem.

SCP 和其中一种 rsync 模式确实需要某种远程登录功能。Rsync 还可以发布它将处理目录的资源。由于 Windows“远程登录”的概念不像在 Linux 下那样成熟,因此这可能更可取。此外,它限制了对目标机器上定义的源/目标的远程访问,而不是允许访问文件系统的任何(授权)部分。

回答by Prabhu R

To transfer over a network more efficiently. Take a look at this articlethat explains efficient data transfer through zero copy

更有效地通过网络传输。看看这篇文章,它解释了通过零复制的高效数据传输