前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android 通过TCP协议上传指定目录文件的方法

Android 通过TCP协议上传指定目录文件的方法

作者头像
砸漏
发布2020-10-16 11:24:02
6880
发布2020-10-16 11:24:02
举报
文章被收录于专栏:恩蓝脚本恩蓝脚本

为了方便客户抓取Log,现通过TCP协议连接指定服务器,传输指定内容,定义指定目录,IP,PORT字段接收参数。直接上代码

代码语言:javascript
复制
 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){ //按行读取文件内容
     os.write(line.getBytes());
     os.write("\r\n".getBytes());//向服务器端发送文件
    }
    buffreader.close();
    inputreader.close();
    }
    fis.close();
 
 
   } catch (JSONException e) {
    e.printStackTrace();
   }
   }
   //关闭客户端输出流,中断上传
   socket.shutdownOutput();
   socket.close();
 
  } catch (IOException e) {
   e.printStackTrace();
  }
  }
 }.start();
 
 }
 
 /**
 * 获取指定目录内所有文件路径
 * @param dirPath 需要查询的文件目录
 */
 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){//判断权限
  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()){//查询子目录
  getAllFiles(fileList,_file.getAbsolutePath());
  } else{
  }
 }
 return fileList;
 }

必须声明一下权限:

<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

设计思路:

1首先通过第三方应用传过来的Log路径,通过遍历该路径,得到该目录下的所有文件,保存到集合中, 2然后通过socker建立通信,通信建立成功后开始传输日志, 3读取指定目录下的日志文件,解析内容传输到服务端, 4日志按行读取, 5内容头部增加TAG以区分不同应用的日志

服务端是因为有现成的软件,所以这里就不做解析了。

LogUtil.uploadLog(“storage/emulated/0/C28Log/CarRecorderLog”,”10.0.16.252″,8088);

总结

到此这篇关于Android 通过TCP协议上传指定目录文件的文章就介绍到这了,更多相关android 上传指定目录文件内容请搜索ZaLou.Cn以前的文章或继续浏览下面的相关文章希望大家以后多多支持ZaLou.Cn!

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档