gpt4 book ai didi

java - 文件传输——Java套接字编程

转载 作者:行者123 更新时间:2023-11-30 09:13:42 25 4
gpt4 key购买 nike

我有以下服务器和客户端代码。客户端尝试将大小为 2000B 的文件(例如“testprgm.txt”)传输到服务器,并将其保存为“Test.txt”。问题是我可以在服务器和客户端上看到字节传输,但是当我在运行这些代码后看到 Test.txt 文件的大小时,它是零。

服务器程序:

import java.io.*;
import java.net.*;

public class ServerTest {

public static void main(String[] args) {
System.out.println("**********Server Program**************");
int byteRead = 0;
try {
ServerSocket serverSocket = new ServerSocket(9999);
if (!serverSocket.isBound())
System.out.println("Sever Socket not Bounded...");
else
System.out.println("Server Socket bounded to Port : " + serverSocket.getLocalPort());

Socket clientSocket = serverSocket.accept();
if (!clientSocket.isConnected())
System.out.println("Client Socket not Connected...");
else
System.out.println("Client Socket Connected : " + clientSocket.getInetAddress());

while (true) {
InputStream in = clientSocket.getInputStream();

OutputStream os = new FileOutputStream("<DESTINATION PATH>/Test.txt");
byte[] byteArray = new byte[100];

while ((byteRead = in .read(byteArray, 0, byteArray.length)) != -1) {
os.write(byteArray, 0, byteRead);
System.out.println("No. of Bytes Received : " + byteRead);
}
synchronized(os) {
os.wait(100);
}
os.close();
serverSocket.close();
//System.out.println("File Received...");
}

} catch (Exception e) {
System.out.println("Server Exception : " + e.getMessage());
e.printStackTrace();
}
}

}

客户端程序:

import java.io.*;
import java.net.*;

public class Clientprgm {

public static void main(String[] args)
{
Socket socket;
try
{
socket = new Socket("SERVER IP ADDRESS>", 9999);
if(!socket.isConnected())
System.out.println("Socket Connection Not established");
else
System.out.println("Socket Connection established : "+socket.getInetAddress());

File myfile = new File("<SOURCE PATH>/testprgm.txt"); //local file path.


if(!myfile.exists())
System.out.println("File Not Existing.");
else
System.out.println("File Existing.");

byte[] byteArray = new byte[1024];

FileInputStream fis = new FileInputStream(myfile);
BufferedInputStream bis = new BufferedInputStream(fis);
OutputStream os = socket.getOutputStream();
int trxBytes =0;
while((trxBytes = bis.read(byteArray, 0, byteArray.length)) !=-1)
{
os.write(byteArray, 0, byteArray.length);
System.out.println("Transfering bytes : "+trxBytes );
}
os.flush();
bis.close();
socket.close();

System.out.println("File Transfered...");
}
catch(Exception e)
{
System.out.println("Client Exception : "+e.getMessage());
}
}
}

最佳答案

我会使用 NIO 进行文件传输,它更短、更高效。这是客户端:

    try (SocketChannel sc = SocketChannel.open(new InetSocketAddress(
hostaddress, 9999));
FileChannel fc = new FileInputStream("test").getChannel()) {
fc.transferTo(0, fc.size(), sc);
}
System.out.println("File Transfered...");

服务器端:

    ServerSocketChannel ss = ServerSocketChannel.open();
ss.bind(new InetSocketAddress("localhost", 9999));
try (SocketChannel sc = ss.accept();
FileChannel fc = new FileOutputStream("test").getChannel()) {
fc.transferFrom(sc, 0, Long.MAX_VALUE);
}

关于java - 文件传输——Java套接字编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20947166/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com