java—通过网络发送多个文件—有时

vxf3dgd4  于 2021-07-05  发布在  Java
关注(0)|答案(0)|浏览(196)

我有一个程序,创建屏幕截图和一些txt文件,并通过套接字发送它。但每次我必须连接到服务器才能发送数据(连接->发送->断开连接)
当前解决方案:服务器:

public class Main {
    public static void main(String[] args) {
        try(ServerSocket serverSocket = new ServerSocket(5000)){
            while(true){
                new Echoer(serverSocket.accept()).start();
            }
        }catch(IOException e){
            System.out.println("Server exception " + e.getMessage());
        }
    }
}

public class Server extends Thread {
    private Socket socket;

    public Server(Socket socket) {
        this.socket = socket;
        System.out.println(this.getName() + " -> " + "CONNECTED");
    }

    @Override
    public void run() {
        try {
            String main = "tests";
            DataInputStream dis;
            BufferedInputStream bis;
            //while (true) {

            bis = new BufferedInputStream(socket.getInputStream());
            dis = new DataInputStream(bis);

            String directory = dis.readUTF(); //read directory
            String dirPath = main + "/" + directory; //main dir
            String tmp = dirPath;

            //create directory
            if (!new File(dirPath).exists()) {
                new File( dirPath).mkdir();
                new File( dirPath + "/o").mkdir();
                new File( dirPath+ "/n").mkdir();
            }

            int fileCount = dis.readInt();
            File[] files = new File[fileCount]; //array for files

            for (File f : files) {
                long fileLength = dis.readLong();
                String fileName = dis.readUTF();

                dirPath = tmp;

                f = new File(dirPath + "/" + fileName);

                FileOutputStream fos = new FileOutputStream(f);
                BufferedOutputStream bos = new BufferedOutputStream(fos);
                for (int j = 0; j < fileLength; j++) bos.write(bis.read());

                bos.close();
                fos.close();
            }
            dis.close();
            System.out.println("Received file");
            //}
        } catch (IOException e) {
            System.out.println("->" + e.getMessage());
        }

    }
}

客户

public class Connection {

    private String path;
    private List<String> files;
    private Socket socket;
    private Config config;

    @SneakyThrows
    public Connection(String path, List<String> files){
        this.config = LoadConfig.getInstance().getConfig();
        this.path = path;
        this.files = files;
        //ip, port from config
        socket = new Socket(
                config.getServer().getUrl(),
                config.getServer().getPort()
        );
    }

    public void send() {

        BufferedOutputStream bos = null;
        DataOutputStream dos = null;
        try {

            bos = new BufferedOutputStream(socket.getOutputStream());
            dos = new DataOutputStream(bos);

            //map paths to files
            List<File> filesList = files.stream().map(e -> new File(path + "/"+ e)).collect(Collectors.toList());
            dos.writeUTF(ComputerInfo.userName()); //send user name
            dos.writeInt(filesList.size()); //send amount of elements

            for(File f: filesList){
                long length = f.length(); //one file size
                dos.writeLong(length);

                String name = f.getName();
                dos.writeUTF(name); //send file name

                FileInputStream fis = new FileInputStream(f);
                BufferedInputStream bis = new BufferedInputStream(fis);

                int theByte = 0;
                while((theByte = bis.read()) != -1) bos.write(theByte);

                bis.close();
            }
            dos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

我想不时地发送文件,这取决于算法返回的内容。意思是不定时发送文件。这意味着发送1个文件等待5分钟发送4个文件等待1秒发送3。。。但我不知道怎么做。我还使用blockingqueue和程序的其他部分创建了客户端,并向其中添加数据。

public class Connection implements Runnable {

    private Socket socket;
    private Config config;
    protected BlockingQueue<Data> queue;
    private BufferedOutputStream bos;
    private DataOutputStream dos;

    @SneakyThrows
    public Connection(BlockingQueue<Data> queue) {
        this.config = LoadConfig.getInstance().getConfig();

        socket = new Socket(
                config.getServer().getUrl(),
                config.getServer().getPort()
        );
        this.queue = queue;
        bos = new BufferedOutputStream(socket.getOutputStream());
    }

    @SneakyThrows
    public void run() {
        dos = new DataOutputStream(bos);

        while (true) {

            if (queue.isEmpty()) {
                continue;
            } else {
                //this.send();
                System.out.println(queue.size());
            }

        }
    }

    @SneakyThrows
    private void send() {
        Data loadData = queue.take();

        List<File> filesList = loadData.getList().stream().map(e -> new File(loadData.getPath() + "/" + e)).collect(Collectors.toList());
        dos.writeUTF(ComputerInfo.userName());
        dos.writeInt(filesList.size());

        for (File f : filesList) {
            long length = f.length();
            dos.writeLong(length);

            String name = f.getName();
            dos.writeUTF(name);

            FileInputStream fis = new FileInputStream(f);
            BufferedInputStream bis = new BufferedInputStream(fis);

            int theByte = 0;
            while ((theByte = bis.read()) != -1) bos.write(theByte);

            bis.close();
            fis.close();

        }
        dos.close();
    }

有什么建议吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题