首页 > 编程语言 > Android socket如何实现文件列表动态访问
2021
09-06

Android socket如何实现文件列表动态访问

idea服务端更新

为了防止代码量上升可能带来的结构杂乱,我们对服务端架构进行优化,server包负责socket服务基础实现,data包负责处理各种安卓端的命令。

将readSocketMsg,writeBackMsg方法单独拿出,创建一个SocketMsg方法类,专门负责数据流的读取与写入。

SocketMsg.java

package lrz.server;

import java.io.*;
import java.net.Socket;
import java.util.ArrayList;

public class SocketMsg {

    public static ArrayList<String> readSocketMsg(Socket socket) throws IOException {
        ArrayList<String> msgList=new ArrayList<String>();
        InputStream inputStream = socket.getInputStream();
        InputStreamReader reader = new InputStreamReader(inputStream, "utf-8");
        BufferedReader bufferedReader=new BufferedReader(reader);
        String lineNumStr = bufferedReader.readLine();
        int lineNum=Integer.parseInt(lineNumStr);
        for(int i=0;i<lineNum;i++){
            String str = bufferedReader.readLine();
            msgList.add(str);
        }
        //读取结束后,输入流不能关闭,此时关闭,会将socket关闭,从而导致后续对socket写操作无法实现
        return msgList;
    }

    public static void writeBackMsg(Socket socket, ArrayList<String> msgBackList) throws IOException {
        BufferedOutputStream os = new BufferedOutputStream(socket.getOutputStream());
        OutputStreamWriter writer=new OutputStreamWriter(os,"UTF-8");
        writer.write(""+msgBackList.size()+"\n");           //未真正写入的输出流,仅仅在内存中
        writer.flush();                                         //写入输出流,真正将数据传输出去
        for(int i=0;i<msgBackList.size();i++){
            writer.write(msgBackList.get(i)+"\n");
            writer.flush();
        }

    }

}

创建NetFileData.java作为文件夹访问的方法类,将exeDir()方法放入其中。

NetFileData.java

package lrz.data;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class NetFileData {

    public static ArrayList<String> exeDir(String cmdBody) throws Exception {
        // TODO Auto-generated method stub
        ArrayList<String> backList=new ArrayList<String>();

        File file = new File(cmdBody);
        File[] listFiles = file.listFiles();
        for(File mfile:listFiles){
            String fileName = mfile.getName();
            long lastModified = mfile.lastModified();//获取文件修改时间
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//给时间格式,例如:2018-03-16 09:50:23
            String fileDate = dateFormat.format(new Date(lastModified));//取得文件最后修改时间,并按格式转为字符串
            String fileSize="0";
            String isDir="1";
            if(!mfile.isDirectory()){//判断是否为目录
                isDir="0";
                fileSize=""+mfile.length();
            }
            backList.add(fileName+">"+fileDate+">"+fileSize+">"+isDir+">");
        }
        return backList;
    }
}

ServerSocket01.java主函数则将以上方法移除,改为调用

package lrz.server;


import lrz.data.NetFileData;

import java.io.*;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Enumeration;

public class ServerSocket01 {
    int port = 8019;// 自定义一个端口,端口号尽可能挑选一些不被其他服务占用的端口,祥见http://blog.csdn.net/hsj521li/article/details/7678880
    static int connect_count = 0;// 连接次数统计
    ArrayList<String>  msgBackList;

    public ServerSocket01() {
        // TODO Auto-generated constructor stub
    }

    public ServerSocket01(int port) {
        super();
        this.port = port;
    }

    private void printLocalIp(ServerSocket serverSocket) {// 枚举打印服务端的IP
        try {
            System.out.println("服务端命令端口prot=" + serverSocket.getLocalPort());
            Enumeration<NetworkInterface> interfaces = null;
            interfaces = NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                NetworkInterface ni = interfaces.nextElement();
                Enumeration<InetAddress> addresss = ni.getInetAddresses();
                while (addresss.hasMoreElements()) {
                    InetAddress nextElement = addresss.nextElement();
                    String hostAddress = nextElement.getHostAddress();
                    System.out.println("本机IP地址为:" + hostAddress);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void work() throws IOException {
        // 注意:由于Socket的工作是阻塞式,Android端Socket的工作必须在新的线程中实现,若在UI主线程中工作会报错

        ServerSocket serverSocket = new ServerSocket(port);
        printLocalIp(serverSocket);
        while (true) {// 无限循环,使之能结束当前socket服务后,准备下一次socket服务

            System.out.println("Waiting client to connect.....");
            Socket socket = serverSocket.accept();// 阻塞式,直到有客户端连接进来,才会继续往下执行,否则一直停留在此代码
            System.out.println("Client connected from: "
                    + socket.getRemoteSocketAddress().toString());

            ArrayList<String> cmdList= SocketMsg.readSocketMsg(socket);
            cmdList.forEach(s -> System.out.println(s));

            String cmdbody=cmdList.get(0);
            try {
                msgBackList= NetFileData.exeDir(cmdbody);
            } catch (Exception e) {
                e.printStackTrace();
            }
            msgBackList.forEach(s -> System.out.println(s));
            SocketMsg.writeBackMsg(socket,msgBackList);
            socket.close();
            System.out.println("当前Socket服务结束");

        }
    }




    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            new ServerSocket01().work();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}


安卓端更新

使用listview显示文件夹内容,并且实现动态访问。

MainActivity.java更新了简单的listview显示,设立互动事件,在点击某个列表部分时向服务端发动指定命令,返回结果后更新列表,实现互动效果。

package com.example.android_app;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.annotation.NonNull;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    public static final String KEY_SERVER_ACK_MSG = "KEY_SERVER_ACK_MSG";
    private  Handler handler = null;
    EditText url,way,dir;
    ListView lv;
    Button submit;
    SocketClient socketClient=null;
    String here;
    ArrayList<String> data;
    int port;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        url=findViewById(R.id.url);
        way=findViewById(R.id.way);
        dir=findViewById(R.id.dir);
        lv=findViewById(R.id.listview);
        submit=findViewById(R.id.submit);

        handler=new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message msg) {

                Bundle data_bundle = msg.getData();
                data=data_bundle.getStringArrayList(KEY_SERVER_ACK_MSG);

                data=dataMaker();
                printAdapter(data);

                return false; }
        });

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                port=Integer.parseInt(way.getText().toString());
                here=dir.getText().toString();
                getdata();
            }
        });


        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                here=here+"/"+data.get(position);
                getdata();
            }
        });
    }

    private void getdata() {
        socketClient=new SocketClient(url.getText().toString(),port,handler);
        socketClient.work(here);
    }



    private ArrayList<String> dataMaker() {
        ArrayList<String> dataResult=new ArrayList<>();
        int i=data.size();
        for (int j = 0; j <i ; j++) {
            String str=data.get(j);
            str=str.substring(0,str.indexOf(">"));
            dataResult.add(str);
        }

        return  dataResult;
    }


    private void printAdapter(ArrayList<String> data) {
        ArrayAdapter<String> arrayAdapter=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,data);
        lv.setAdapter(arrayAdapter);
    }

}


SocketClient.java无变动

activity_main.xml布局添加listview,代替原先的textview

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:id="@+id/url"
            android:text="服务端ip"/>
        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:id="@+id/way"
            android:text="8019"/>

    </LinearLayout>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/dir"
        android:text="d://"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="submit"
        android:id="@+id/submit"/>
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listview"/>
</LinearLayout>

补充一点:

安卓端访问的ip在服务端中查看,ip为服务端网络ip,我是红色圈中的,可以都试一下,一般都是倒数第二个长得像ip的这个,port在服务端主函数中设置,可以是任何数,8019为常用端

以上就是Android socket如何实现文件列表动态访问的详细内容,更多关于Android socket实现列表动态访问的资料请关注自学编程网其它相关文章!

编程技巧