Sending end part of the code (thread) :
Public void the send () {
New Thread (new Runnable () {
@ Override
Public void the run () {
Try {
OutputStream OutputStream=mSocket. GetOutputStream ();
InputStream InputStream=mSocket. GetInputStream ();
//the file name and file size is sent to the receiver
OutputStream. Write (getHeaderString (mHeader). GetBytes (UTF_8));
OutputStream. Flush ();
SendMessage (mHandler, 10, "sent the file name and file size");
Byte [] startData=https://bbs.csdn.net/topics/new byte [buf_size];
Int len=inputStream. Read (startData);
If (len!=1) {
String start=new String (startData, 0, len);
If (start. Equals (" start ")) {
For (the FileInfo the FileInfo: mHeader getFileInfos ()) {
Int sentLen=0;
Byte [] sendData publishes the event=https://bbs.csdn.net/topics/new byte [file_buf_size];
Long totalSize=the fileInfo. GetFileLength ();
InputStream fileInputStream=new fileInputStream (new File (the fileInfo. GetFileName ()));
SendMessage (mHandler, 10, "is sending:" + the fileInfo. GetFileName ());
While ((len=fileInputStream. Read (sendData publishes the event))!=1) {
OutputStream. Write (sendData publishes the event, 0, len);
SentLen +=len;
Int progress=(int) (sentLen * 100/totalSize);
(L.e rror (" file "+ the fileInfo getFileName () +") total size: "+ totalSize +" has received file (" + + "progress) :" + sentLen);
}
FileInputStream. Close ();
}
OutputStream. Close ();
}
}
} the catch (FileNotFoundException e) {
e.printStackTrace();
L.e rror (" FileNotFoundException: "+ um participant etMessage ());
} the catch (IOException e) {
e.printStackTrace();
L.e rror (" IOException: "+ um participant etMessage ());
} the finally {
Try {
MSocket. Close ();
} the catch (IOException e) {
e.printStackTrace();
}
Completion of sendMessage (mHandler, 10, "");
}
}
}). The start ();
}
The receiver part of the code (thread) :
Public void the receive () {
New Thread (new Runnable () {
@ Override
Public void the run () {
//whether receive completely
Boolean isFinish=false;
If (mSocket!=null) {
Try {
Final InputStream is=mSocket. GetInputStream ();
OutputStream OS=mSocket. GetOutputStream ();
//get the file name and file size
Byte [] headerData=https://bbs.csdn.net/topics/new byte [buf_size];
Int len=is. Read (headerData);
If (len!=1) {
Header Header=getHeader (new String (headerData, 0, len, UTF_8));
If (header!=null & amp; & The header. GetFileInfos ()!=null & amp; & The header. GetFileInfos (). The size () & gt; 0 {
SendMessage (mHandler, ReceiverActivity INIT_ADAPTER, header getFileInfos ());
//to inform the sender start sending
OS. Write (" start ". GetBytes (UTF_8));
OS. The flush ();
//is ready to receive
String, fileDir,=FileUtils. GetReceiveFilesPath ();
for (int i=0; iThe FileInfo the FileInfo=header. GetFileInfos (). The get (I);
The String filename=the fileInfo. GetFileName ();
Long totalSize=the fileInfo. GetFileLength ();
SendMessage (mHandler, 10, "is ready to receive: file name:" + filename + + totalSize "file size:");
//if the file does not exist, create it
The File File=new File (filename, fileDir, +);
if (! File. The exists ()) {
File. GetParentFile (). The file.mkdirs ();
file.createNewFile();
}
DataOutputStream DOS=new DataOutputStream (new FileOutputStream (file));
Byte [] recvData=https://bbs.csdn.net/topics/new byte [file_buf_size];
Int receiveLen=0;
While ((len=is. Read (recvData))!=1) {
DOS. Write (recvData, 0, len);
ReceiveLen +=len;
Int progress=(int) (receiveLen * 100/totalSize);
SendMessage (mHandler, ReceiverActivity REFRESH_PROGRESS, I, progress);
L.e rror (" file (" + filename + ") total size: "+ totalSize +" has received file (" + + "progress) :" + receiveLen);
If (progress==100) {
break;
}
}
//file size inconsistent
If (receiveLen! TotalSize)={
SendMessage (mHandler, 10, "file size is not consistent");
}
dos.flush();
DOS. Close ();
}
//receive completely
IsFinish=true;
OS. The close ();
Is the close ();
}
}
} the catch (FileNotFoundException e) {
e.printStackTrace();
IsFinish=false;
SendMessage (mHandler, 10, "receiving failure:" + um participant etMessage ());
} the catch (IOException e) {
e.printStackTrace();
IsFinish=false;
SendMessage (mHandler, 10, "receiving failure:" + um participant etMessage ());
} the finally {
Try {
//close the Socket
MSocket. Close ();
} the catch (IOException e) {
IsFinish=false;
e.printStackTrace();
}
If (isFinish) {
SendMessage (mHandler, 10, "receive completely");
} else {
SendMessage (mHandler, 10, "receive the error");
}
}
}
}
}). The start ();
}
If something can be improved or is wrong, please grant instruction!
CodePudding user response:
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull