Как отправить имя файла с файлом с помощью сокетов в Java? [Дубликат]

На этот вопрос уже есть ответ здесь:

Java многократная передача файла через сокет 2 ответа

У меня есть следующий код, который передает файл через сокеты. Как мне отправить имя файла?

Socket socket = new Socket("localhost", port);//machine name, port number
File file = new File(fileName);
// Get the size of the file
long length = file.length();
if (length > Integer.MAX_VALUE) 
{
    System.out.println("File is too large.");
}
byte[] bytes = new byte[(int) length];
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());

int count;

while ((count = bis.read(bytes)) > 0) 
{
    out.write(bytes, 0, count);
}

out.flush();
out.close();
fis.close();
bis.close();
socket.close();

Ответы на вопрос(3)

Ваш ответ на вопрос