欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 通過(guò)TCP協(xié)議上傳指定目錄文件的方法

 更新時(shí)間:2020年03月25日 10:19:44   作者:祥子Gyx  
這篇文章主要介紹了Android 通過(guò)TCP協(xié)議上傳指定目錄文件的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

為了方便客戶(hù)抓取Log,現(xiàn)通過(guò)TCP協(xié)議連接指定服務(wù)器,傳輸指定內(nèi)容,定義指定目錄,IP,PORT字段接收參數(shù)。直接上代碼

 public static void uploadLog(final String dirPath, final String IP, final int port ) {
 JSONArray fileList = new JSONArray();
 final JSONArray allFiles = getAllFiles(fileList,dirPath);
 if(allFiles==null)return;
 new Thread(){
  @Override
  public void run() {
  super.run();
  try {
   Socket socket=new Socket(IP,port);
   if(!socket.isConnected())return; //判斷是否建立連接
   OutputStream os = socket.getOutputStream();
   int index = dirPath.lastIndexOf("/")+1;
   os.write(dirPath.substring(index).getBytes());//TAG
   os.write("\r\n".getBytes());
 
   for (int i=0;i<allFiles.length();i++){
   try {
    JSONObject o = (JSONObject) allFiles.get(i);
    String path = o.getString("path");
    String name = o.getString("name");
    FileInputStream fis=new FileInputStream(path);
    if(fis!=null){
    InputStreamReader inputreader = new InputStreamReader(fis);
    BufferedReader buffreader = new BufferedReader(inputreader);
    String line;
    while ((line=buffreader.readLine())!=null){ //按行讀取文件內(nèi)容
     os.write(line.getBytes());
     os.write("\r\n".getBytes());//向服務(wù)器端發(fā)送文件
    }
    buffreader.close();
    inputreader.close();
    }
    fis.close();
 
 
   } catch (JSONException e) {
    e.printStackTrace();
   }
   }
   //關(guān)閉客戶(hù)端輸出流,中斷上傳
   socket.shutdownOutput();
   socket.close();
 
  } catch (IOException e) {
   e.printStackTrace();
  }
  }
 }.start();
 
 }
 
 /**
 * 獲取指定目錄內(nèi)所有文件路徑
 * @param dirPath 需要查詢(xún)的文件目錄
 */
 public static JSONArray getAllFiles(JSONArray fileList,String dirPath) {
 File f = new File(dirPath);
 if (!f.exists()) {//判斷路徑是否存在
  return null;
 }
 File[] files = f.listFiles();
 
 if(files==null){//判斷權(quán)限
  return null;
 }
 for (File _file : files) {//遍歷目錄
  if(_file.isFile()){
 
  String _name=_file.getName();
  String filePath = _file.getAbsolutePath();//獲取文件路徑
  int end=_file.getName().lastIndexOf('.');
  String fileName = _file.getName().substring(0,end);//獲取文件名
  try {
   JSONObject _fInfo = new JSONObject();
   _fInfo.put("name", fileName);
   _fInfo.put("path", filePath);
   fileList.put(_fInfo);
 
 
  }catch (Exception e){
  }
  } else if(_file.isDirectory()){//查詢(xún)子目錄
  getAllFiles(fileList,_file.getAbsolutePath());
  } else{
  }
 }
 return fileList;
 }

必須聲明一下權(quán)限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

設(shè)計(jì)思路:

  1首先通過(guò)第三方應(yīng)用傳過(guò)來(lái)的Log路徑,通過(guò)遍歷該路徑,得到該目錄下的所有文件,保存到集合中,

  2然后通過(guò)socker建立通信,通信建立成功后開(kāi)始傳輸日志, 

  3讀取指定目錄下的日志文件,解析內(nèi)容傳輸?shù)椒?wù)端, 

  4日志按行讀取, 

  5內(nèi)容頭部增加TAG以區(qū)分不同應(yīng)用的日志

服務(wù)端是因?yàn)橛鞋F(xiàn)成的軟件,所以這里就不做解析了。

LogUtil.uploadLog("storage/emulated/0/C28Log/CarRecorderLog","10.0.16.252",8088);

總結(jié)

到此這篇關(guān)于A(yíng)ndroid 通過(guò)TCP協(xié)議上傳指定目錄文件的文章就介紹到這了,更多相關(guān)android 上傳指定目錄文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論