我有以下服务器和客户端代码。客户端尝试将大小为 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/