iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android 通过TCP协议上传指定目录文件
  • 406
分享到

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

tcp协议Android 2022-06-06 13:06:29 406人浏览 薄情痞子
摘要

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

为了方便客户抓取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();
    }
    
    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;
    }

必须声明一下权限:


设计思路:
  1首先通过第三方应用传过来的Log路径,通过遍历该路径,得到该目录下的所有文件,保存到集合中,
  2然后通过socker建立通信,通信建立成功后开始传输日志,
  3读取指定目录下的日志文件,解析内容传输到服务端,
  4日志按行读取,
  5内容头部增加TAG以区分不同应用的日志
服务端是因为有现成的软件,所以这里就不做解析了。
LogUtil.uploadLog("storage/emulated/0/C28Log/CarRecorderLog","10.0.16.252",8088);


作者:祥子Gyx


--结束END--

本文标题: Android 通过TCP协议上传指定目录文件

本文链接: https://www.lsjlt.com/news/29286.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作