Android Open Source - AndFileTranser Server Thread






From Project

Back to project page AndFileTranser.

License

The source code is released under:

GNU General Public License

If you think the Android project AndFileTranser listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.server;
/*from ww  w.j  a  v  a  2 s  . c o  m*/
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.res.Resources;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class ServerThread implements Runnable
{

  private Handler mHandler = null;
  private Socket mSocket = null;
  private List<Socket> mSocketList = null;
  /** ??????? **/
  private String mSavePath;//????
  private final String TAG = "ServerThread";
  private UpdateInfo mInfo;//??????????
  private final String DIV_WEN = "?";//????????
  private final String DIV_XING = "*";//????????
  private final String DIV_DOLAR = "$";//????????
  private final String FILE_FLAG = "???";//????????
  private final int BUF_SIZE = 8192;//?????????
  private boolean mIsData = false;//??true???????????false??????????
  private Resources mRes;//???

  public ServerThread(Handler handler, Socket socket, String path, List<Socket> list, Resources res) {
    System.out.println("?????????");
    this.mHandler = handler;
    this.mSocket = socket;
    this.mSavePath = path;
    this.mSocketList = list;
    this.mRes = res;
  }

  @Override
  public void run() {

    boolean flag = true;
    BufferedInputStream socketIn = null;
    BufferedOutputStream fileOut = null;
    PrintWriter socketOut = null;
    //???socket????????
    try {
      socketIn = new BufferedInputStream(mSocket.getInputStream());
      socketOut = new PrintWriter(mSocket.getOutputStream());
    } catch (IOException e) {
      Log.e(TAG, "IOException" + e);
    }

    while (flag) {
      byte[] buf = new byte[BUF_SIZE];
      int length = 0;
      long totalLen = 0;
      long accLen = 0;
      String fileName = "";
      try {
        while ((length = socketIn.read(buf)) != -1) {

          if (!mIsData) {//??????
            String info = new String(buf, 0, length);
            System.out.println("??????????:" + info);
            if (info.substring(0, 2).equals(FILE_FLAG)) {
              int xingIndex = info.indexOf(DIV_XING);
              int wenIndex = info.indexOf(DIV_WEN);
              int dolarIndex = info.indexOf(DIV_DOLAR);
              fileName = info.substring(xingIndex + 1, wenIndex);
              //???????????
              fileOut = new BufferedOutputStream(new FileOutputStream(mSavePath + File.separator
                  + fileName));
              //???????
              String fileLen = info.substring(wenIndex + 1, dolarIndex);
              totalLen = new Long(fileLen).longValue();
              accLen = 0;
              System.out.println("?????" + fileName + "   ???????:" + fileLen);
              mInfo = new UpdateInfo();
              sendMsg(fileName, totalLen, accLen);
              /** ???? **/
              socketOut.println("has receive...." + info);
              socketOut.flush();
              mIsData = true;
            }
          }
          else {//????  
            accLen += length;
            System.out.println("???????");
            if (accLen > 0) {
              //System.out.println(new String(buf));
              fileOut.write(buf, 0, length);
              sendMsg(fileName, totalLen, accLen);
              /** ???? **/
              if (accLen % BUF_SIZE == 0 || accLen == totalLen) {
                socketOut.println("has receive...." + accLen);
                socketOut.flush();
              }
              if (accLen == totalLen) {
                socketOut.println("receive completed:" + fileName);
                socketOut.flush();
                fileOut.close();
                mIsData = false;
                sendMsg(fileName + mRes.getString(R.string.acccomplete));
                break;
              }
            }
          }
        }
      } catch (IOException e) {
        Log.i("??????(??):", mSocket.toString());
        mSocketList.remove(mSocket);
        String str = fileName.equals("") ? fileName + mRes.getString(R.string.connfailed) : "";
        File file = new File(mSavePath + File.separator + fileName);
        file.delete();
        sendMsg(mSocket.getInetAddress().toString() + mRes.getString(R.string.connbreak) + str);
        flag = false;
        break;
      } catch (NumberFormatException e) {
        Log.e("NumberFormatException", e.toString());
      } catch (NullPointerException e) {
        Log.e("NullPointerException", e.toString());
      } catch (Exception e) {
        Log.e("Exception", e.toString());
      }

      if (length == -1) {
        Log.i("??????(????):", mSocket.toString());
        mSocketList.remove(mSocket);
        String str = fileName.equals("") ? "" : fileName + mRes.getString(R.string.connfailed);
        File file = new File(mSavePath + File.separator + fileName);
        file.delete();
        sendMsg(mSocket.getInetAddress().toString() + mRes.getString(R.string.connbreak) + str);
        flag = false;
        break;
      }

    }
  }

  /**
   * ???????
   * 
   * @param str
   */
  private void sendMsg(String str) {
    Message msg = new Message();
    msg.obj = str;
    msg.what = MainActivity.MSG_CONN;
    mHandler.sendMessage(msg);
  }

  /**
   * ???????
   * 
   * @param fileName
   *        ?????
   * @param totalLen
   *        ????????
   * @param accLen
   *        ???????
   */
  private void sendMsg(String fileName, long totalLen, long accLen) {
    mInfo.setAcceptLength(accLen);
    mInfo.setFileName(fileName);
    mInfo.setTotalLength(totalLen);
    mInfo.setIp(mSocket.getInetAddress().toString());
    Message msg = new Message();
    msg.obj = mInfo;
    msg.what = MainActivity.MSG_TRANS;
    mHandler.sendMessage(msg);
  }
}




Java Source Code List

com.client.BrowseFileAdapter.java
com.client.MainActivity.java
com.client.SendFileAdapter.java
com.client.ShowFileActivity.java
com.client.UpdateInfo.java
com.server.AcceptFileAdapter.java
com.server.MainActivity.java
com.server.ServerThread.java
com.server.Server.java
com.server.UpdateInfo.java