各位,小弟最近在做一个socke发送文件的程序,服务器端接收到文件就写文件,客户端发送文件,当发送单个文件时没有问题,发送多个文件时会出现有前一个文件(文件A)结尾部分会写到下一个文件(文件B)的情况,但是文件A是完整的,麻烦各位指教一下是怎么回事?我用的是InputStream 读取的文件内容,OutputStream写入socket中的。
麻烦各位指教一下啦!!

解决方案 »

  1.   


    public void sendFile(File mFile)    {       Log.d(TAG,"---------sendFile = " + mFile.getName() + "fileSize = " + mFile.length());       try {           Thread.sleep(200);       } catch (InterruptedException e2) {           // TODO Auto-generated catch block           e2.printStackTrace();       }//     try {//         socketFile.setTcpNoDelay(false);//     } catch (SocketException e2) {//         // TODO Auto-generated catch block//         e2.printStackTrace();//     }       int i = 0;       try {           mInStrFile = new FileInputStream(mFile);           int readSize = 0;           int readFileSize = 0;           byte data[] = new byte[1228];           int[] per = new int[2];           do           {              if(mRunning)              {                  readSize = mInStrFile.read(data);                  if(readSize != -1)                  {                     {                         mOutStrFile.write(data, 0, readSize);                     }                     i++;                  }                 }              else              {                  Log.d(TAG, "---break");                  break;              }
    //更新界面
    //            readTotalSize += readSize;//            readFileSize += readSize;//            Message msg = new Message();//            msg.what = ConstData.MSG_AUTO_BACKUPS_PERCENT;//            percent = (int) ((long)readTotalSize * 100 / backupsFileSize);//            per[i % 2] = percent;//            msg.arg1 = percent;//            if(per[0] != per[1])//            {//                mHandler.sendMessage(msg);//            }           }while(readSize != -1);                      //socketFile.shutdownOutput();           //mOutStrFile.//         socketFile.setTcpNoDelay(true);           mOutStrFile.flush();//         mInStrFile.close();//         mInStrFile = null;           Log.d(TAG, "*********" + mFile.getName() + " send succeed, fileSize is:" + readFileSize);           //发送文件结束标志           sendMsg(ConstData.AMP_REQUEST_BACKUPS_FILE_END_LEN,                   ConstData.AMP_REQUEST_BACKUPS_FILE_END, ConstData.FILE_END_FLAG);                  } catch (FileNotFoundException e1) {           // TODO Auto-generated catch block           e1.printStackTrace();           Log.d(TAG,"-------fff");       } catch (IOException e) {           // TODO Auto-generated catch block           e.printStackTrace();           Log.d(TAG,"-------iii " + e.toString());       }          }
      

  2.   

    在一个循环中调用的sendFile
    for(int i = 0; i < fileNum; i++)
    {
        sendFile(fileList(i));
    }
      

  3.   


    在一个循环中调用的sendFilefor(int i = 0; i < fileNum; i++)
    {
        sendFile(fileList(i));
    }