客户端
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流中,服务器负责读流中数据,写入目标路径,完成文件的传输
原文地址:https://www.cnblogs.com/lkldeblog/p/9095432.html
时间: 2024-09-30 08:14:56