How do I send file name with file using sockets in Java? [duplicate]

妖精的绣舞 提交于 2019-12-02 10:54:14

You can invent your own protocol for your socket. If all you need is a filename and data, DataOutputStream.writeUTF is easiest:

BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
try (DataOutputStream d = new DataOutputStream(out)) {
    d.writeUTF(fileName);
    Files.copy(file.toPath(), d);
}

The peer must use the same protocol, of course:

BufferedInputStream in = new BufferedInputStream(socket.getInputStream());
try (DataInputStream d = new DataInputStream(in)) {
    String fileName = d.readUTF();
    Files.copy(d, Paths.get(fileName));
}
Patashu

Use a character that can never be in a file name - such as a null (0x00, \0, whatever you want to call it). Then send a 64 bit integer that indicates how long, in bytes, the file is (make sure you don't run into buffer overflows, little endian/big endian issues, etc... just test all edge cases). Then send the file data. Then the ending socket will know which part is the file name, the file length and the file data, and will even be ready for the next file name if you want to send another.

(if file names can be arbitrary characters including control characters, ouch! Maybe send a 64 bit integer length of file name, the file name, a 64 bit integer length of file data, the file data, repeat ad infinitum?)

EDIT: To send a 64 bit integer over a socket, send its constituent bytes in a specific order, and make sure sender and receiver agree on the order. One example of how to do this is How to convert a Java Long to byte[] for Cassandra?

I tried to wrap a buffer which cause MalfuctionUTF and putting it on try-with resource closes the underlining socket stream and cause connection reset exception
Following code worked for me

Client

DataOutputStream d = new DataOutputStream(out);
        d.writeUTF(filename);
        d.writeLong(length);

Server

DataInputStream d = new DataInputStream(in);
filename = d.readUTF();
fileLength = d.readLong();
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!