一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - Java使用Socket通信傳輸文件的方法示例

Java使用Socket通信傳輸文件的方法示例

2020-11-23 10:53kongxx Java教程

這篇文章主要介紹了Java使用Socket通信傳輸文件的方法,結(jié)合實(shí)例形式分析了java socket編程實(shí)現(xiàn)文件傳輸操作的相關(guān)技巧,需要的朋友可以參考下

本文實(shí)例講述了Java使用Socket通信傳輸文件的方法。分享給大家供大家參考,具體如下:

前面幾篇文章介紹了使用Java的Socket編程和NIO包在Socket中的應(yīng)用,這篇文章說(shuō)說(shuō)怎樣利用Socket編程來(lái)實(shí)現(xiàn)簡(jiǎn)單的文件傳輸。

這里由于前面一片文章介紹了NIO在Socket中的應(yīng)用,所以這里在讀寫文件的時(shí)候也繼續(xù)使用NIO包,所以代碼看起來(lái)會(huì)比直接使用流的方式稍微復(fù)雜一點(diǎn)點(diǎn)。

下面的示例演示了客戶端向服務(wù)器端發(fā)送一個(gè)文件,服務(wù)器作為響應(yīng)給客戶端回發(fā)一個(gè)文件。這里準(zhǔn)備兩個(gè)文件E:/test/server_send.log和E:/test/client.send.log文件,在測(cè)試完畢后在客戶端和服務(wù)器相同目錄下會(huì)多出兩個(gè)文件E:/test/server_receive.log和E:/test/client.receive.log文件。

下面首先來(lái)看看Server類,主要關(guān)注其中的sendFile和receiveFile方法。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.googlecode.garbagecan.test.socket.nio;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.FileChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MyServer4 {
  private final static Logger logger = Logger.getLogger(MyServer4.class.getName());
  public static void main(String[] args) {
    Selector selector = null;
    ServerSocketChannel serverSocketChannel = null;
    try {
      // Selector for incoming time requests
      selector = Selector.open();
      // Create a new server socket and set to non blocking mode
      serverSocketChannel = ServerSocketChannel.open();
      serverSocketChannel.configureBlocking(false);
      // Bind the server socket to the local host and port
      serverSocketChannel.socket().setReuseAddress(true);
      serverSocketChannel.socket().bind(new InetSocketAddress(10000));
      // Register accepts on the server socket with the selector. This
      // step tells the selector that the socket wants to be put on the
      // ready list when accept operations occur, so allowing multiplexed
      // non-blocking I/O to take place.
      serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
      // Here's where everything happens. The select method will
      // return when any operations registered above have occurred, the
      // thread has been interrupted, etc.
      while (selector.select() > 0) {
        // Someone is ready for I/O, get the ready keys
        Iterator<SelectionKey> it = selector.selectedKeys().iterator();
        // Walk through the ready keys collection and process date requests.
        while (it.hasNext()) {
          SelectionKey readyKey = it.next();
          it.remove();
          // The key indexes into the selector so you
          // can retrieve the socket that's ready for I/O
          doit((ServerSocketChannel) readyKey.channel());
        }
      }
    } catch (ClosedChannelException ex) {
      logger.log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
      logger.log(Level.SEVERE, null, ex);
    } finally {
      try {
        selector.close();
      } catch(Exception ex) {}
      try {
        serverSocketChannel.close();
      } catch(Exception ex) {}
    }
  }
  private static void doit(final ServerSocketChannel serverSocketChannel) throws IOException {
    SocketChannel socketChannel = null;
    try {
      socketChannel = serverSocketChannel.accept();
      receiveFile(socketChannel, new File("E:/test/server_receive.log"));
      sendFile(socketChannel, new File("E:/test/server_send.log"));
    } finally {
      try {
        socketChannel.close();
      } catch(Exception ex) {}
    }
  }
  private static void receiveFile(SocketChannel socketChannel, File file) throws IOException {
    FileOutputStream fos = null;
    FileChannel channel = null;
    try {
      fos = new FileOutputStream(file);
      channel = fos.getChannel();
      ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
      int size = 0;
      while ((size = socketChannel.read(buffer)) != -1) {
        buffer.flip();
        if (size > 0) {
          buffer.limit(size);
          channel.write(buffer);
          buffer.clear();
        }
      }
    } finally {
      try {
        channel.close();
      } catch(Exception ex) {}
      try {
        fos.close();
      } catch(Exception ex) {}
    }
  }
  private static void sendFile(SocketChannel socketChannel, File file) throws IOException {
    FileInputStream fis = null;
    FileChannel channel = null;
    try {
      fis = new FileInputStream(file);
      channel = fis.getChannel();
      ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
      int size = 0;
      while ((size = channel.read(buffer)) != -1) {
        buffer.rewind();
        buffer.limit(size);
        socketChannel.write(buffer);
        buffer.clear();
      }
      socketChannel.socket().shutdownOutput();
    } finally {
      try {
        channel.close();
      } catch(Exception ex) {}
      try {
        fis.close();
      } catch(Exception ex) {}
    }
  }
}

下面是Client程序代碼,也主要關(guān)注sendFile和receiveFile方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.googlecode.garbagecan.test.socket.nio;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MyClient4 {
  private final static Logger logger = Logger.getLogger(MyClient4.class.getName());
  public static void main(String[] args) throws Exception {
    new Thread(new MyRunnable()).start();
  }
  private static final class MyRunnable implements Runnable {
    public void run() {
      SocketChannel socketChannel = null;
      try {
        socketChannel = SocketChannel.open();
        SocketAddress socketAddress = new InetSocketAddress("localhost", 10000);
        socketChannel.connect(socketAddress);
        sendFile(socketChannel, new File("E:/test/client_send.log"));
        receiveFile(socketChannel, new File("E:/test/client_receive.log"));
      } catch (Exception ex) {
        logger.log(Level.SEVERE, null, ex);
      } finally {
        try {
          socketChannel.close();
        } catch(Exception ex) {}
      }
    }
    private void sendFile(SocketChannel socketChannel, File file) throws IOException {
      FileInputStream fis = null;
      FileChannel channel = null;
      try {
        fis = new FileInputStream(file);
        channel = fis.getChannel();
        ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
        int size = 0;
        while ((size = channel.read(buffer)) != -1) {
          buffer.rewind();
          buffer.limit(size);
          socketChannel.write(buffer);
          buffer.clear();
        }
        socketChannel.socket().shutdownOutput();
      } finally {
        try {
          channel.close();
        } catch(Exception ex) {}
        try {
          fis.close();
        } catch(Exception ex) {}
      }
    }
    private void receiveFile(SocketChannel socketChannel, File file) throws IOException {
      FileOutputStream fos = null;
      FileChannel channel = null;
      try {
        fos = new FileOutputStream(file);
        channel = fos.getChannel();
        ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
        int size = 0;
        while ((size = socketChannel.read(buffer)) != -1) {
          buffer.flip();
          if (size > 0) {
            buffer.limit(size);
            channel.write(buffer);
            buffer.clear();
          }
        }
      } finally {
        try {
          channel.close();
        } catch(Exception ex) {}
        try {
          fos.close();
        } catch(Exception ex) {}
      }
    }
  }
}

首先運(yùn)行MyServer4類啟動(dòng)監(jiān)聽,然后運(yùn)行MyClient4類來(lái)向服務(wù)器發(fā)送文件以及接受服務(wù)器響應(yīng)文件。運(yùn)行完后,分別檢查服務(wù)器和客戶端接收到的文件。

希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 2020韩国三级理论在线观看 | 免费成年网站 | 娇妻终于接受了3p的调教 | 国内精品视频免费观看 | 我与么公激情性完整视频 | 好吊色永久免费视频大全 | 欧美视频精品一区二区三区 | chinese壮直男gay老年人 | 亚洲同性男男gay1069 | 亚洲v成人天堂影视 | 五月天中文在线 | 亚洲精品第二页 | 欧美日韩在线观看区一二 | 免费看h片的网站 | 厨房play黄瓜进入 | pregnantxxx孕交 | 青青草影院在线观看 | 国产99精品成人免费视频 | 色综合久久中文字幕 | 99毛片| 奇米影视久久 | 日本三级在丈面前被耍了 | 射逼视频 | 久久久精品免费免费直播 | 91热国内精品永久免费观看 | 国产一区二区三区四 | 91在线视频导航 | 国产精品一区二区久久 | 亚洲国产在线播放 | 国内偷拍第一页 | 四虎最新永久免费网址 | 色涩导航 | 亚洲男女在线 | 日本老妇乱子伦中文视频 | 隔壁老王国产精品福利 | 四虎影视国产精品婷婷 | 国产成人在线免费视频 | 国产一卡2卡3卡四卡精品网 | 青草国产在线观看 | 99久久久久国产 | 久久精品成人免费网站 |