原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://zhangfengzhe.blog.51cto.com/8855103/1726488
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
|
import java.io.Closeable; import java.io.FileInputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; /** * 这个类的基本思路是,读取本地文件到缓冲区 * 因为通道只能操作缓冲区 */ public class DownloadFileProcesser implements Closeable { private ByteBuffer buffer = ByteBuffer.allocate(8 * 1024); private FileChannel fileChannel ; public DownloadFileProcesser() { try{ FileInputStream fis = new FileInputStream("/Users/cross/PractiseProject/weixinTouxiang.py"); fileChannel = fis.getChannel(); }catch(Exception e){ e.printStackTrace(); } } public int readFile2Buffer() throws IOException{ int count = 0; buffer.clear(); count = fileChannel.read(buffer); buffer.flip(); return count; } public ByteBuffer getByteBuffer(){ return buffer; } @Override public void close() throws IOException { fileChannel.close(); } }
|
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
|
import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; import java.util.Iterator; public class ServerMain { public static void main(String[] args) throws IOException { ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.configureBlocking(false); serverSocketChannel.socket().bind(new InetSocketAddress(8887)); Selector selector = Selector.open(); serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); while (true) { selector.select(); Iterator
|
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
|
import java.io.IOException; import java.io.RandomAccessFile; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.SocketChannel; import java.util.Iterator; /** * Created by cross on 2017/7/13. */ public class Client4DownloadFile implements Runnable { //标示 private String name; private FileChannel fileChannel; public Client4DownloadFile(String name , RandomAccessFile randomAccessFile){ this.name = name; this.fileChannel = randomAccessFile.getChannel(); } private ByteBuffer buffer = ByteBuffer.allocate(8 * 1024); @Override public void run() { try { SocketChannel sc = SocketChannel.open(); Selector selector = Selector.open(); sc.configureBlocking(false); sc.register(selector, SelectionKey.OP_CONNECT); sc.connect(new InetSocketAddress("127.0.0.1",8887)); while(true){ selector.select(); Iterator
|
1
2
3
4
5
6
7
8
9
10
|
import java.io.File; import java.io.FileNotFoundException; import java.io.RandomAccessFile; /** * Created by cross on 2017/7/13. */ public class ClientMain { public static void main(String[] args) throws FileNotFoundException { File file = new File("/Users/cross/PractiseProject/2.py"); RandomAccessFile raf = new RandomAccessFile(file,"rw"); Client4DownloadFile client4DownloadFile = new Client4DownloadFile("1" , raf); new Thread(client4DownloadFile).start(); } }
|