局域网传输文件Demo

Posted lkldeblog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了局域网传输文件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("\u5F00\u59CB\u4F20\u8F93\u6587\u4EF6");
        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("\u5F00\u542F\u4F20\u8F93\u6587\u4EF6\u670D\u52A1");
        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流中,服务器负责读流中数据,写入目标路径,完成文件的传输

 


以上是关于局域网传输文件Demo的主要内容,如果未能解决你的问题,请参考以下文章

[vscode]--HTML代码片段(基础版,reactvuejquery)

用MFC做文件传输工具时,怎么做出选择文件的界面?

网盘工具2:局域网ftp文件传输

VS code自定义用户代码片段snippet

企业网盘居然支持高速局域网文件传输工具(速度可达20M)

ftp局域网传输速度由啥决定?