문제

I am trying to write a server that accepts files and write it in certain directory using DataInputStream and BufferedInputStream.

The server gets 'user name(string)' 'number of files(int)' 'file name(string)' 'size of each file(long)' and 'contents of file which is uninterpreted bytes(byte[])'

and if everything is successful then, I am supposed to send boolean value.

But the problem is that it is not receiving file correctly.

From time to time I get 'broken pipe' error message or the file is corrupted after I receive.

Fixed the problem..

도움이 되었습니까?

해결책

One small thing which may be related to your problem. You should be decrementing your file size variable by the number of bytes actually read, instead of the number of bytes requested to be read:

       while(fileSize>0){
            if(fileSize < byteSize)
                byteSize = (int)fileSize;
            int byteRead = din.read(b, 0, byteSize);
            fos.write(b);
            fileSize -= byteRead; // <-- See here
        }

다른 팁

You might be getting this error if when reading the input, the sender closes the connection. It probably has nothing to do with your code. The sender might have timed out, closed the connection before the transfer has finished, or many other things.

Take a look at this related question: How to fix java.net.SocketException: Broken pipe?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top