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

作者:祥子Gyx 时间:2023-11-07 23:34:11 

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


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协议上传指定目录文件的方法

来源:https://blog.csdn.net/weixin_35649059/article/details/105068257

标签:android,tcp协议,上传
0
投稿

猜你喜欢

  • java实现多线程的两种方式继承Thread类和实现Runnable接口的方法

    2022-07-24 04:27:18
  • java实现滑动验证解锁

    2023-06-02 12:16:36
  • Android 图片选择详解及实例代码

    2021-09-13 13:13:19
  • 详解C#设置Excel数据自适应行高、列宽的2种情况

    2022-10-21 23:46:29
  • Java之NIO基本简介

    2021-12-20 22:15:44
  • 简单了解java标识符的作用和命名规则

    2022-06-18 17:49:09
  • C#实现启用与禁用本地网络的方式小结【3种方式】

    2022-04-21 18:45:14
  • Java+Nginx实现POP、IMAP、SMTP邮箱代理服务

    2023-11-26 10:31:47
  • C#中DataSet转化为实体集合类的方法

    2022-05-09 00:50:46
  • VisualStudio Community2019在安装的过程中无法进入安装界面的解决方法

    2023-02-09 11:24:34
  • Android View背景选择器编写技巧

    2023-09-16 21:37:43
  • IDEA2022.2的简介、下载与安装、配置教程

    2023-01-13 21:21:03
  • 使用SpringBoot跨系统调用接口的方案

    2022-10-14 21:35:18
  • Springboot+ElementUi实现评论、回复、点赞功能

    2022-06-16 04:34:00
  • android 分辨率适配的方法

    2023-03-09 09:21:47
  • Android实现上传文件功能的方法

    2022-01-18 11:01:02
  • Dubbo retries 超时重试机制的问题原因分析及解决方案

    2023-11-29 08:22:38
  • 深入探索Java常量池

    2022-12-28 00:55:53
  • Java二维数组查找功能代码实现

    2023-01-04 19:47:17
  • WinForm实现移除控件某个事件的方法

    2021-09-25 02:57:58
  • asp之家 软件编程 m.aspxhome.com