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();
}
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.
解决方案
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.