http://blog.csdn.net/java2000_net/archive/2008/06/10/2529015.aspx
http://www.java2000.net/viewthread.jsp?tid=6079
http://topic.csdn.net/u/20080610/02/6e33be0a-152f-481a-a10e-f9c11c8fd9ad.html?seed=577363641
1. package test.io;
2.
3. import java.nio.channels.*;
4. import java.nio.charset.*;
5. import java.net.*;
6. import java.io.*;
7. import java.util.*;
8. import java.nio.*;
9.
10. public class FileServer {
11. private int port = 8050;
12.
13. private ServerSocketChannel serverSocketChannel;
14.
15. private Charset charset = Charset.forName("GBK");
16.
17. private Selector selector = null;
18.
19. public FileServer() throws IOException {
20. selector = Selector.open();
21. serverSocketChannel = ServerSocketChannel.open();
22. serverSocketChannel.socket().setReuseAddress(true);
23. serverSocketChannel.socket().bind(new InetSocketAddress(port));
24. System.out.println("服务器启动");
25. }
26.
27. /* 编码过程 */
28. public ByteBuffer encode(String str) {
29. return charset.encode(str);
30. }
31.
32. /* 解码过程 */
33. public String decode(ByteBuffer bb) {
34. return charset.decode(bb).toString();
35. }
36.
37. /* 服务器服务方法 */
38. public void service() throws IOException {
39. serverSocketChannel.configureBlocking(false);
40. serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
41. /** 外循环,已经发生了SelectionKey数目 */
42. while (selector.select() > 0) {
43. /* 得到已经被捕获了的SelectionKey的集合 */
44. Iterator iterator = selector.selectedKeys().iterator();
45. while (iterator.hasNext()) {
46. SelectionKey key = null;
47. try {
48. key = (SelectionKey) iterator.next();
49. iterator.remove();
50. if (key.isAcceptable()) {
51. ServerSocketChannel ssc = (ServerSocketChannel) key.channel();
52. SocketChannel sc = ssc.accept();
53. System.out.println("客户端机子的地址是 " + sc.socket().getLocalAddress() + " 客户端机机子的端口号是 " + sc.socket().getLocalPort());
54. sc.configureBlocking(false);
55. ByteBuffer buffer = ByteBuffer.allocate(1024);
56. sc.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE, buffer);
57. }
58. if (key.isReadable()) {
59. reveice(key);
60. }
61. if (key.isWritable()) {
62. send(key);
63. }
64. } catch (IOException e) {
65. e.printStackTrace();
66. try {
67. if (key != null) {
68. key.cancel();
69. key.channel().close();
70. }
71. } catch (ClosedChannelException cex) {
72. e.printStackTrace();
73. }
74. }
75. }
76. /* 内循环完 */
77. }
78. /* 外循环完 */
79. }
80.
81. /* service方法完 */
82. public void reveice(SelectionKey key) throws IOException {
83. if (key == null)
84. return;
85. ByteBuffer buff = (ByteBuffer) key.attachment();
86. SocketChannel sc = (SocketChannel) key.channel();
87. buff.limit(buff.capacity());
88. buff.position(0);
89. sc.read(buff);
90. buff.flip();
91. String reviceData = decode(buff);
92. }
93.
94. /* 发送文件 */
95. public void send(SelectionKey key) {
96. if (key == null)
97. return;
98. ByteBuffer buff = (ByteBuffer) key.attachment();
99. SocketChannel sc = (SocketChannel) key.channel();
100. String data = decode(buff);
101. if (data.indexOf("get") == -1)
102. return;
103. String subStr = data.substring(data.indexOf(" "), data.length());
104. System.out.println("截取之后的字符串是 " + subStr);
105. FileInputStream fileInput = null;
106. try {
107. fileInput = new FileInputStream(subStr);
108. FileChannel fileChannel = fileInput.getChannel();
109. fileChannel.transferTo(0, fileChannel.size(), sc);
110. } catch (IOException e) {
111. e.printStackTrace();
112. } finally {
113. try {
114. fileInput.close();
115. } catch (IOException ex) {
116. ex.printStackTrace();
117. }
118. }
119. }
120.
121. public static void main(String[] args) throws IOException {
122. new FileServer().service();
123. }
124. }