Socket – based file transfer desktop procedures

GitHub

1. Program construction

Procedures using JavafX based on socket development of a Java desktop procedures

1. You can customize the startup port

2. No packet data size is optional

3. Select the storage directory

4. Support the client to carry directories

5, the custom logging module in addition to the JDK does not introduce dependencies

2. Program use

1. Start the service according to the setting

2. Client Demo

public void transClient(String path) throws Exception {

        File file = new File(path);

        File[] list = file.listFiles();

        int bufferSize = 1024 * 100;

        byte[] buf = new byte[bufferSize];

        for (File f : list) {

            try (
                    Socket socket = new Socket("127.0.0.1".8888);
                    DataInputStream dis = new DataInputStream(new FileInputStream(f.getAbsolutePath()));
                    DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
            ) {
                // Customize the append directory
                String appendPath = "test";
                dos.writeUTF( appendPath+File.separator + f.getName());
                dos.flush();

                dos.writeLong(f.length());

                dos.flush();

                int len = 0;
                while((len = dis.read(buf)) ! = -1) {
                    dos.write(buf, 0, len);
                }
                dos.flush();
                dis.close();
                dos.close();
                socket.close();

                log.info(f.getName() + "-- Transmission completed");
            } catch(Exception e) { log.info(ExceptionUtils.getStackTrace(e)); }}}Copy the code

3. Traversal files in the directory

 public static void loopFileByPath(List<File> list, String path) {

        File file = new File(path);

        File[] files = file.listFiles();

        for (File f : files) {
            if (f.isFile()) {
                list.add(f);
            } else{ loopFileByPath(list, f.getPath()); }}}Copy the code