使用socket java无法在send文件中获取接收文件循环 [英] Can't get out the receive file loop in send file using socket java
问题描述
我正在开发客户端 - 服务器应用程序。从客户端,我将文件发送到服务器。
这是我在服务器上读取文件的代码:
I am developing client-server app. From client, I send a file to server. Here is my code about read file at server:
private void readfile() throws Exception{
String filename="data.xml";
FileOutputStream fos = new FileOutputStream(filename);
BufferedOutputStream out = new BufferedOutputStream(fos);
BufferedReader buff_read = new BufferedReader(new InputStreamReader(input,"UTF-8"));
byte[] buffer=new byte[1024];
String line;
int count;
while((count=input.read(buffer))>0){
fos.write(buffer,0,count);
fos.flush();
}
System.out.println("het");
fos.close();
}
但我无法摆脱while循环。字符串hetnerver在控制台屏幕显示,而我可以通过记事本++读取文件data.xml意味着文件发送成功。
你能帮我解决这个问题。
对不起我糟糕的英语。
But I can't get out of the while loop. String "het" nerver show in console screen while I can read file data.xml by notepad++ mean that the file sent success. Can you help me solve this problem. Sorry for my poor Englisg.
推荐答案
read()方法在流结束时返回-1,套接字表示对等方已关闭连接。如果你没有得到-1,则对等体没有关闭连接。
The read() method returns -1 at end of stream, which for a socket means the peer has closed the connection. If you're not getting -1, the peer isn't closing the connection.
这篇关于使用socket java无法在send文件中获取接收文件循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!