0%

FileChannel 从文件中读写数据。

FileChannelTest.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class FileChannelTest {

public static void main(String[] args) throws IOException {
FileChannel inputFileChannel = new FileInputStream("file.text absolute path").getChannel();
ByteBuffer readBuffer = ByteBuffer.allocate(100);
inputFileChannel.read(readBuffer);
readBuffer.flip();
for (int i = 0; i < readBuffer.limit(); i++) {
System.out.println((char) readBuffer.get());
}

FileChannel outputFileChannel = new FileOutputStream("file.text absolute path", true).getChannel();
ByteBuffer writeBuffer = ByteBuffer.wrap("TOM".getBytes());
outputFileChannel.write(writeBuffer);

inputFileChannel.close();
outputFileChannel.close();
}
}
阅读全文 »

1.变量:

capacity: 指定了可以存储在缓冲区中的最大数据容量,实际上,它指定了底层数组的大小,或者至少是指定了准许我们使用的底层数组的容量。
position: 下一次读取或写入的位置。(每次调用get put方法+1)
limit:指定还有多少数据需要取出,或者还有多少空间可以放入数据。(每次调用get put方法时候,通过position与limit对比)

2.方法:

get:position + 1
put:position + 1
flip:limit = position position = 0 mark = -1
clear:limit = capacity position = 0 mark = -1
mark: mark = position
reset:position = m;
duplicate:浅拷贝(每个缓存区的上界、容量、位置等属性是各自独立的)

阅读全文 »

Send.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Send {
public static String EXCHANGE_NAME = "test_exchange_topic";

public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setVirtualHost("/zhang");
factory.setUsername("zhang");
factory.setPassword("zhang");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "topic");
String message = "message";
channel.basicPublish(EXCHANGE_NAME, "item.broadcast", null, message.getBytes());
channel.close();
connection.close();
}
}
阅读全文 »

Send.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Send {
public static String EXCHANGE_NAME = "test_exchange_direct";

public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setVirtualHost("/zhang");
factory.setUsername("zhang");
factory.setPassword("zhang");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "direct");
String message = "message";
channel.basicPublish(EXCHANGE_NAME, "insert", null, message.getBytes());
channel.close();
connection.close();
}
}
阅读全文 »

Send.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Send {
public static String EXCHANGE_NAME = "test_exchange_fanout";

public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setVirtualHost("/zhang");
factory.setUsername("zhang");
factory.setPassword("zhang");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
String message = "message";
channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes());
channel.close();
connection.close();
}
}
阅读全文 »

Server.java

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
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8081);
while (true) {
Socket socket = serverSocket.accept();
new Thread(new Runnable() {
@Override
public void run() {
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String str = "";
while ((str = bufferedReader.readLine()) != null) {
if (str.equals("1")) {
Thread.sleep(5 * 1000);
}
System.out.println(str);
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
}

Client.java

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 8081);
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
while (true) {
String str = bufferedReader.readLine();
bufferedWriter.write(str);
bufferedWriter.write("\n");
bufferedWriter.flush();
}
}
}

Java Socket源码地址​github.com


Send.java

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
public class Send {
public static String QUEUE_NAME = "test_queue";

public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setVirtualHost("/zhang");
factory.setUsername("zhang");
factory.setPassword("zhang");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(
QUEUE_NAME, false, false,
false, null
);
for (int i = 0; i < 50; i++) {
String message = "message:\t" + i;
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println(message);
try {
Thread.sleep(i * 10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
channel.close();
connection.close();
}
}
阅读全文 »

Send.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class Send {
public static String QUEUE_NAME = "test_queue";

public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setVirtualHost("/zhang");
factory.setUsername("zhang");
factory.setPassword("zhang");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(
QUEUE_NAME, false, false,
false, null
);
String message = "message";
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
channel.close();
connection.close();
}
}

阅读全文 »

1
2
3
4
5
6
7
8
9
10
11
server {
listen 80;
listen 443 ssl;
server_name localhost;
ssl_certificate /SSLPATH/zhang.crt;
ssl_certificate_key /SSLPATH/zhang.key;
location / {
# TOMCAT
proxy_pass http://localhost:5566;
}
}

OPENSSL 自签证书

1.(.key)文件

通常指私钥。

openssl genrsa -des3 -out zhang.key 1024

2.(.csr)文件

csr 是Certificate Signing Request的缩写,即证书签名请求,这不是证书,可以简单理解成公钥,生成证书时要把这个提交给权威的证书颁发机构。

openssl req -new -key zhang.key -out zhang.csr

阅读全文 »