1. 程式人生 > >局域網傳輸文件Demo

局域網傳輸文件Demo

exceptio true jpa ble fileinput main reat tcl out

客戶端

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