实现两台服务器间文件复制,需要通过网络传输文件。下面我将简单介绍一下Java实现文件复制的方法与实现步骤。
步骤
步骤一:建立Socket套接字
通过Socket
对象建立连接,用于发送数据。
Socket socket = new Socket(InetAddress.getByName(IP), port);
其中,IP为目标服务器的具体IP地址,port为目标服务器的端口号。
步骤二:建立文件输入输出流
通过FileInputStream
将本地文件读入内存,然后通过OutputStream
将文件数据发送到目标服务器上。具体实现可以参考以下代码示例:
File file = new File("src/file.txt");
FileInputStream fis = new FileInputStream(file);
OutputStream os = socket.getOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) != -1) {
os.write(buffer, 0, length);
}
os.flush();
os.close();
fis.close();
步骤三:建立文件服务器
在目标服务器上监听客户端的请求信息,通过ServerSocket
来实现。
ServerSocket serverSocket = new ServerSocket(port);
Socket socket = serverSocket.accept();
其中,端口号port需与发送端保持一致。
步骤四:建立文件输入输出流
服务器获取到客户端发送的信息后,通过InputStream
将数据读取到内存,然后通过FileOutputStream
写入到指定的目录中。具体实现可以参考以下代码:
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream("dst/file.txt");
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) != -1) {
fos.write(buffer, 0, length);
}
fos.flush();
fos.close();
is.close();
示例
示例一:本地复制到远程服务器
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
public class CopyFileClient {
public static void main(String[] args) throws Exception {
String IP = "192.168.0.100";
int port = 8888;
File file = new File("src/file.txt");
FileInputStream fis = new FileInputStream(file);
Socket socket = new Socket(InetAddress.getByName(IP), port);
OutputStream os = socket.getOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) != -1) {
os.write(buffer, 0, length);
}
os.flush();
os.close();
fis.close();
socket.close();
}
}
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class CopyFileServer {
public static void main(String[] args) throws Exception {
int port = 8888;
ServerSocket serverSocket = new ServerSocket(port);
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream("dst/file.txt");
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) != -1) {
fos.write(buffer, 0, length);
}
fos.flush();
fos.close();
is.close();
socket.close();
serverSocket.close();
}
}
示例二:远程服务器复制到本地
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
public class CopyFileClient {
public static void main(String[] args) throws Exception {
String IP = "192.168.0.100";
int port = 8888;
Socket socket = new Socket(InetAddress.getByName(IP), port);
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream("dst/file.txt");
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) != -1) {
fos.write(buffer, 0, length);
}
fos.flush();
fos.close();
is.close();
socket.close();
}
}
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class CopyFileServer {
public static void main(String[] args) throws Exception {
int port = 8888;
File file = new File("src/file.txt");
FileInputStream fis = new FileInputStream(file);
ServerSocket serverSocket = new ServerSocket(port);
Socket socket = serverSocket.accept();
OutputStream os = socket.getOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) != -1) {
os.write(buffer, 0, length);
}
os.flush();
os.close();
fis.close();
socket.close();
serverSocket.close();
}
}
以上就是Java实现两台服务器间文件复制的完整攻略。其中示例中的文件路径需要根据实际情况进行修改。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:java实现两台服务器间文件复制的方法 - Python技术站