即时编码客户端服务器在Java中,客户端使用writeUTF发送文件名,服务器接收数据,然后从服务器获取文件。你能给我一些建议吗?我不知道我做错了什么,它不工作。谢谢:)使用writeUTF()从DataOutputStream发送文件名
这种方法在Client.java
public void enviarFichero(String file, String server, int port)
{
// open a socket
Socket socket = openSocket(server, port);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] data = Files.readAllBytes(file);
DataOutputStream dos = new DataOutputStream(baos.write(data));
dos.writeUTF();
}
这是Server.java
ByteArrayInputStream bais = new ByteArrayInputStream(almacen);
DataInputStream dis = new DataInputStream(bais);
filename = dis.readUTF();
System.out.println("Receiving files "+filename+" from "+dirIPCliente.getHostName()+":"+clientPort);
// Open destination file
filtroDestino = new BufferedOutputStream(new FileOutputStream(nombreFichero));