我创建了一个接受来自客户端的连接的服务器套接字,当建立连接时,使用写入字节的 OutputStream 将图像传输给它。我的问题是如何在关闭套接字连接之前检查 OutputStream 是否已完成写入字节,因为有时并非所有图像都正确传输。这是我正在使用的代码:
File photoFile = new File(getHeader); //getHeader is the file that i have to transfer
int size2 = (int) photoFile.length();
byte[] bytes2 = new byte[size2];
try {
BufferedInputStream buf = new BufferedInputStream(new FileInputStream(photoFile));
buf.read(bytes2, 0, bytes2.length);
buf.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
client.getOutputStream().write(bytes2, 0, size2); //client is the server socket
谢谢