最近在用Java NIO做迁移文件,本地测试是可以的,但是本地连接测试环境,并使用Nginx转发时(本地连本地,用Nginx转发也不行),出现服务端能收能发,但是客户端收到的是null(如果是对象字符串,所有的属性都是null),调试了好久不知道怎么弄,请大神们帮我
Nginx配置如下:
stream {
#-------------transferfile------------------------
upstream transferfile {
hash $remote_addr consistent;
server 192.168.1.91:1234 max_fails=3 fail_timeout=30s;
}
server {
listen 8091;
proxy_connect_timeout 30s;
proxy_timeout 600s;
proxy_pass transferfile;
}
}
服务端发送信息:
private void responseData(SocketChannel socketChannel, String response) {
ByteBuffer buffer = ByteBuffer.wrap(response.getBytes());
try {
socketChannel.write(buffer);
buffer.clear();
socketChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
客户端接收信息
public String receiveData(SocketChannel socketChannel) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
String response = "";
try {
ByteBuffer buffer = ByteBuffer.allocate(1024);
byte[] bytes;
int count = 0;
while ((count = socketChannel.read(buffer)) >= 0) {
buffer.flip();
bytes = new byte[count];
buffer.get(bytes);
baos.write(bytes);
buffer.clear();
}
bytes = baos.toByteArray();
response = new String(bytes, "UTF-8");
} finally {
try {
baos.close();
} catch (Exception ex) {
}
if(socketChannel != null){
socketChannel.close();
}
}
return response;
}