Home > Back-end >  Java socket when transferring large files (2 g), in the process of transmission is always throw Java
Java socket when transferring large files (2 g), in the process of transmission is always throw Java

Time:10-09

Using Java socket large file transfers, when transferring large files will throw an exception: ` Java. IO. UTFDataFormatException: malformed input around 1 byte `

sender

While ((I=fin. Read (outBytes)) & gt; 0 {
DataOutputStream. WriteUTF (" 003 ");
Long point=position * outBytes. Length;
DataOutputStream. WriteLong (point);//the current cursor position, the receiver from here began to write
DataOutputStream. WriteInt (I);//data length
DataOutputStream. Write (outBytes, 0, I);//the sender to perform here thrown broke pipe
DataOutputStream. Flush ();
Position the position of=+ 1;
}

The receiver of * * * *
` ` ` Java
Operate=in readUTF ();//receiver throws an exception here
Case "003" : {
//handling to send the file (receive)
Try (RandomAccessFile RandomAccessFile=new RandomAccessFile (enPath, "rw")) {
//the cursor position
Long pos=in readLong ();
//data length
Int inputSize=in readInt ();
Byte [] eoutBytes=new byte (inputSize);
In the read (eoutBytes);
//the cursor to the specified location
RandomAccessFile. Seek (pos);

RandomAccessFile. Write (eoutBytes, 0, inputSize);
The pre=pos - inputSize;
TransferFile. SetCur (pre);
}
return true;
}
` ` `

The sender send 003 cycle, cursor position, the length of the data, data content
The receiver loop reads the utf, long, int, byte []

Because in the small files cannot be thrown ` Java IO. UTFDataFormatException: malformed input around 1 byte `, when transferring large files will always waiting for a specific time to throw an exception, so wonder if there is a queue is full cause ` operate=. In readUTF () ` read the wrong data. But I couldn't find the problem

CodePudding user response:

May also be lost package, the data is incomplete,
  • Related