我在通过套接字发送文件(不一定是 txt 文件)时遇到问题。我有 2 个类:服务器、客户端。当我从套接字输出流中读取并想在文件中写入字节时,它看起来工作但当我打开文件时它什么都没有。(损坏的显示大小= 0 kb)。我还希望它通过套接字传输所有类型的文件。我不想使用appache commons net。这是我的代码服务器类
FileOutputStream toFile1 = new FileOutputStream(f);
BufferedOutputStream toFile= new BufferedOutputStream(toFile1);
BufferedInputStream bis=new BufferedInputStream(incoming.getInputStream());
byte[]buffer=new byte[2048];
int bytesRead=0;
while((bytesRead = bis.read(buffer)) >= 0)
{
toFile.write(buffer, 0, bytesRead);
}
toFile.close();
toFile1.close();
bis.close();
out.println("226 Connection Closed");
out.flush();
}
客户端类
BufferedOutputStream output = new BufferedOutputStream(socket.getOutputStream());
byte[] buffer = new byte[60*2024];
int bytesRead = 0;
while ((bytesRead = input.read(buffer,0,60*1024)) != -1) {
output.write(buffer, 0, bytesRead);
}