• 局域网传输文件Demo


    客户端

    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.net.Socket;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JLabel;
    
    
    public class FileClient extends JFrame {
    
        private JPanel contentPane;
        public JButton btnNewButton;
        public JLabel labelStatus;
    
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        FileClient frame = new FileClient();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        /**
         * Create the frame.
         */
        public FileClient() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 450, 300);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            setContentPane(contentPane);
            contentPane.setLayout(null);
            
            btnNewButton = new JButton("u5F00u59CBu4F20u8F93u6587u4EF6");
            btnNewButton.addActionListener(new BtnNewButtonActionListener());
            btnNewButton.setBounds(125, 97, 163, 23);
            contentPane.add(btnNewButton);
            
            labelStatus = new JLabel("");
            labelStatus.setBounds(125, 147, 163, 28);
            contentPane.add(labelStatus);
        }
        private class BtnNewButtonActionListener implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                try {
                    Socket socket = new Socket("10.12.50.10",8888);
                    new ClientThd(socket).start();
                    labelStatus.setText("文件传输完毕");
                } catch (Exception e1) {
                    e1.printStackTrace();
                } 
            }
        }
        
        class ClientThd extends Thread{
            Socket socket;
    
            public ClientThd(Socket socket) {
                this.socket = socket;
            }
            public void run() {
                File f = new File("D:\aaa.txt");
                DataInputStream dis = null;
                DataOutputStream dos = null;
                try {
                    dis = new DataInputStream(new FileInputStream(f));//读磁盘文件
                    dos = new DataOutputStream(socket.getOutputStream());//socket输出流
                    //缓冲区的大小为1024
                    byte[] buff = new byte[1024];    
                    int icurrPos = 0;
                    while((icurrPos=dis.read(buff, 0, 1024))!=-1){
                        dos.write(buff, 0, icurrPos);
                        dos.flush();
                    }     
                    
                    
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.*;
    import java.net.*;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JLabel;
    
    
    public class FileServer extends JFrame {
    
        private JPanel contentPane;
        public JButton button;
        ServerSocket server;
        public JLabel labelStatus;
    
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        FileServer frame = new FileServer();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        /**
         * Create the frame.
         */
        public FileServer() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 450, 300);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            setContentPane(contentPane);
            contentPane.setLayout(null);
            
            button = new JButton("u5F00u542Fu4F20u8F93u6587u4EF6u670Du52A1");
            button.addActionListener(new ButtonActionListener());
            button.setBounds(125, 108, 154, 23);
            contentPane.add(button);
            
            labelStatus = new JLabel("");
            labelStatus.setBounds(127, 162, 152, 23);
            contentPane.add(labelStatus);
        }
    
        private class ButtonActionListener implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                try {
                    server = new ServerSocket(8888);
                    new serverThd().start();
                    labelStatus.setText("服务已开启");
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        }
        class serverThd extends Thread{
            public void run() {
                Socket s = null;
                DataInputStream dis = null;
                DataOutputStream dos = null;
                try {
                    s = server.accept();
                    
                    dis = new DataInputStream(s.getInputStream());
    //目标路径 dos
    = new DataOutputStream(new FileOutputStream("D:\aa.txt")); byte[] buff = new byte[1024]; int icurrPos = 0; while((icurrPos=dis.read(buff, 0, 1024))!=-1){ dos.write(buff,0,icurrPos); dos.flush(); } } catch (Exception e) { e.printStackTrace(); } finally{ try { dis.close(); dos.close(); s.close(); } catch (Exception e) { e.printStackTrace(); } } } } }

    写本地文件到socket流中,服务器负责读流中数据,写入目标路径,完成文件的传输

  • 相关阅读:
    [Algorithm] Flowerbox: Dynamic programming
    [CSS 3] Overflow & sticky problem
    [Algorithm] Bottom-up Dynamic programming approch
    高频交易的理论基石 —— 市场微观结构
    固态硬盘windows10安装笔记
    本土高频量化交易大败局:市场所有参与者共同作用的恶性循环
    CER.LIVE Report: Top 25 Decentralized Exchanges by Cybersecurity Score
    如何判断两条轨迹(或曲线)的相似度?
    皮尔逊相关系数实现相似K线及其性能优化
    视频:高盛王牌交易员揭露交易的真相
  • 原文地址:https://www.cnblogs.com/lkldeblog/p/9095432.html
Copyright © 2020-2023  润新知