1、服務器端代碼
- package demo.net;
-
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.SocketException;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
-
- /**
- * 下載服務器,采用UDP協議,傳送的過程中可能會丟包,導致下載的文件不完整
- */
- public class DownloadServer {
- // 提供服務
- public void service() {
- try {
- // 創建本機指定端口8289的服務器
- DatagramSocket dataSocket = new DatagramSocket(8289);
- // 線程池,固定有十個線程
- ExecutorService ThreadPool = Executors.newFixedThreadPool(10);
-
- while (true) {// 不斷接收來自客戶端的請求
- byte[] buff = new byte[101];// 文件名長度不超過50
- DatagramPacket dataPacket = new DatagramPacket(buff, buff.length);
- dataSocket.receive(dataPacket);// 等待接收來自客戶端的數據包
- // 接收到數據包,開一個線程為該客戶服務
- ThreadPool.execute(new WorkThread(dataPacket));
- }
- } catch (SocketException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- // 內部類,為每個客戶提供服務
- private class WorkThread implements Runnable {
- private DatagramPacket packet;
- private DatagramSocket dataSocket;
-
- public WorkThread(DatagramPacket packet) {
- this.packet = packet;
- try {// 創建本機可以端口的DatagramSocket
- dataSocket = new DatagramSocket();
- } catch (SocketException e) {
- e.printStackTrace();
- }
- }
-
- // 獲取可以下載的文件列表傳送給客戶端
- private void showFiles() {
- File files = new File("upload_download");
- File[] allFile = files.listFiles();// 獲取所有文件
- StringBuffer message = new StringBuffer();
- for (File f : allFile) {
- if (f.isFile()) {
- message.append(f.getName());
- message.append('\n');
- }
- }
- // 構造響應數據包
- byte[] response = message.toString().getBytes();
- DatagramPacket dataPacket = new DatagramPacket(response, response.length, packet.getAddress(), packet.getPort());
- try {// 發送
- dataSocket.send(dataPacket);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- // 下載指定的文件
- private void download(String fileName) {
- try {
- InputStream in = new FileInputStream("upload_download/" + fileName);
- DatagramPacket dataPacket;
- byte[] response = new byte[60000];// 每次發送60000字節
- while (true) {
- int len = in.read(response, 0, response.length);
- dataPacket = new DatagramPacket(response, len, packet.getAddress(), packet.getPort());
- dataSocket.send(dataPacket);// 發送
- if (in.available() == 0)// 發送完畢
- break;
- }
- in.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- @Override
- public void run() {
- // 獲取客戶端傳送過來的數據
- byte[] data = packet.getData();
- // 表示客戶端點擊顯示文件按鈕,該請求是要得到所有可以下載的文件
- if (data[0] == 0)
- showFiles();
- else if (data[0] == 1)// 表示客戶端的請求是下載請求
- download(new String(data, 1, packet.getLength()).trim());
- else
- System.out.println("請求錯誤");
- }
- }
-
- public static void main(String[] args) {
- new DownloadServer().service();
- }
- }
說明:DatagramSocket 類是基於UDP協議,服務器端默認提供下載的文件存放在當前目錄下的upload_download文件夾中。服務器采用線程池能同時為多個用戶服務
2、客戶端代碼
- package demo.net;
-
- import java.awt.BorderLayout;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.InetAddress;
-
- import javax.swing.*;
-
- /**
- * 下載客戶端
- */
- public class DownloadClient extends JFrame {
- // 顯示可下載的文件
- private JTextArea textArea = new JTextArea();
-
- private JPanel panel = new JPanel();
- // 下載時保存文件
- private JFileChooser saveFile = new JFileChooser(".");
-
- private JButton showButton = new JButton("顯示文件");
- private JButton downloadButton = new JButton("下載...");
- // 下載時填入要下載的文件名,注意文件名必須是textArea顯示的文件名
- private JTextField downloadFile = new JTextField("");
-
- private DatagramSocket dataSocket=null;
-
- public DownloadClient() {
- // frame 的基本設置
- this.setTitle("下載客戶端");
- this.setVisible(true);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setSize(400, 500);
- this.setLayout(new BorderLayout());
- this.setResizable(false);
-
- // 設置不可編輯
- textArea.setEditable(false);
-
- panel.setLayout(new GridLayout(3, 2, 5, 5));
- panel.add(new JLabel("點擊按鈕顯示可下載的文件"));
- panel.add(showButton);
- panel.add(downloadFile);
- panel.add(downloadButton);
-
- // 組件加入frame中
- add(new JScrollPane(textArea));
- add(panel, BorderLayout.SOUTH);
-
- // saveFile只能打開目錄
- saveFile.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
-
- // 顯示文件按鈕注冊事件
- showButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- showButton.setEnabled(false);
- downloadButton.setEnabled(false);
- showFiles();
- showButton.setEnabled(true);
- downloadButton.setEnabled(true);
- }
- });
-
- // 下載按鈕注冊事件
- downloadButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- showButton.setEnabled(false);
- downloadButton.setEnabled(false);
- downloadFile();
- showButton.setEnabled(true);
- downloadButton.setEnabled(true);
- }
- });
- }
-
- // 顯示文件
- private void showFiles() {
- try {
- if (dataSocket == null)
- dataSocket = new DatagramSocket();
- // 創建發送數據包並發送給服務器
- byte[] request = { 0 };
- DatagramPacket requestPacket = new DatagramPacket(request, request.length, InetAddress.getLocalHost(), 8289);
- dataSocket.send(requestPacket);
-
- // 接收服務器的數據包,顯���在textArea中
- byte[] receive = new byte[1024 * 1024];
- DatagramPacket receivePacket = new DatagramPacket(receive, receive.length);
- dataSocket.receive(receivePacket);
- String str = new String(receivePacket.getData(), 0, receivePacket.getLength());
- textArea.setText(str);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- // 下載文件
- private void downloadFile() {
- // 獲取要下載的文件名
- String fileName = downloadFile.getText().trim();
- // 所有可以下載的文件
- String allFiles = textArea.getText();
- // 文件名為空
- if (fileName == null || "".equals(fileName))
- JOptionPane.showMessageDialog(null, "請選中正確的文件名", "文件名錯誤", JOptionPane.WARNING_MESSAGE);
- // 文件名是在可以下載的文件中
- else if (allFiles.contains((fileName + '\n'))) {
- saveFile.showSaveDialog(null);
- File f = saveFile.getSelectedFile();// 獲取選中的文件夾
- if (f.exists()) {
- // 檢測該文件是否已經存在於目錄中
- String[] fileNames = f.list();
- boolean exit = false;
- for (String name : fileNames)
- if (name.equals(fileName)) {
- exit = true;
- break;
- }
-
- if (exit)// 如果要下載的文件已經存在
- JOptionPane.showMessageDialog(null, "此文件已經存在", "請選擇另外的文件下載", JOptionPane.WARNING_MESSAGE);
- else {
- // 發送的請求
- byte[] request = (new String(new byte[] { 1 }) + fileName).getBytes();
- try {
- if (dataSocket == null)
- dataSocket = new DatagramSocket();
- // 創建發送數據包並發送給服務器
- DatagramPacket requestPacket = new DatagramPacket(request, request.length, InetAddress.getLocalHost(), 8289);
- dataSocket.send(requestPacket);
-
- // 接收服務器的數據包,把文件保存在選中的文件夾中
- OutputStream out = new FileOutputStream(f.getAbsolutePath() + "/" + fileName, true);
- byte[] receive = new byte[60000];// 每次接收60000字節
- DatagramPacket receivePacket;
- // 不斷接收來自服務器的數據包
- while (true) {
- receivePacket = new DatagramPacket(receive, receive.length);
- dataSocket.receive(receivePacket);
- out.write(receivePacket.getData(), 0, receivePacket.getLength());// 輸出流把文件內容輸出到文件中
- out.flush();
- if (receivePacket.getLength() != receive.length)
- break;
- }
- out.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- } else
- // 選擇的文件夾不存在
- JOptionPane.showMessageDialog(null, "請選擇正確的存儲路徑", "存儲路徑錯誤", JOptionPane.WARNING_MESSAGE);
-
- } else {// 文件名錯誤
- JOptionPane.showMessageDialog(null, "請選擇正確的文件名", "文件名錯誤", JOptionPane.WARNING_MESSAGE);
- }
- }
-
- public static void main(String[] args) {
- new DownloadClient();
- }
- }
說明:客戶端下載時先點擊顯示文件,然後輸入要下載的文件名,最後點擊下載按鈕、選擇保存路徑
3、運行時先開服務器,然後再開客戶端。運行結果:
點擊顯示文件按鈕、輸入01.jpg後:
點擊下載按鈕後、選擇保存路徑
點擊保存按鈕後,文件下載到指定目錄下