0
我在通过套接字发送文件(不一定是txt文件)时遇到问题。我有2个类:服务器,客户端。当我从套接字输出流中读取数据并希望写在一个文件中的字节,它看起来工作,但是当我打开文件它什么都没有(损坏显示大小= 0 kb)。我也希望它通过套接字传输所有类型的文件。我不想使用appache commons net。 这里是我的代码 服务器类在ftp协议中通过套接字传输文件
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();
}
Client类
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);
}
谢谢all.I必须刷新并关闭我的客户端的输出。我还有一个问题:如果在工作中,客户端变得不耐烦,服务器应该做什么? – Proud
@Proud关闭数据连接,中止操作,并通过命令连接报告错误。也许删除部分上传,取决于您的要求。如果写入命令通道的异常写入,请关闭该命令。 – EJP