学习NIO写了1个测试HTTP服务器,出现了接受POST参数时丢失的情况,大约POST发送的汉字超过150多个,就丢失了,反复改大ByteBuffer 也没有用,就怀疑是不是有什么限制,请教各位环境: win + jdk1.6
linux + jdk1.6都如此,请大家帮忙看看,谢谢代码如下:..
private ByteBuffer buffer = ByteBuffer.allocateDirect(819200); //定义缓冲区
..........
//读取接收的数据 protected String readData(SelectionKey key) throws Exception {
SocketChannel socketChannel = (SocketChannel) key.channel();
int count = 0;
buffer.clear(); // 清空buffer
String recStr = null;
int len = 0;
// 读取所有的数据
try {
if ((count = socketChannel.read(buffer)) > 0) {
buffer.flip();
len = buffer.limit();
byte[] temp = new byte[len];
buffer.get(temp);
recStr = new String(temp);
buffer.compact();
}
buffer.clear(); // 清空buffer
if (count < 0) { // count<0,说明服务端已经断开
socketChannel.close();
}
} catch (Exception e) {
if (socketChannel != null) {
socketChannel.close();
}
e.printStackTrace();
}
socketChannel = null;
return recStr;
}
linux + jdk1.6都如此,请大家帮忙看看,谢谢代码如下:..
private ByteBuffer buffer = ByteBuffer.allocateDirect(819200); //定义缓冲区
..........
//读取接收的数据 protected String readData(SelectionKey key) throws Exception {
SocketChannel socketChannel = (SocketChannel) key.channel();
int count = 0;
buffer.clear(); // 清空buffer
String recStr = null;
int len = 0;
// 读取所有的数据
try {
if ((count = socketChannel.read(buffer)) > 0) {
buffer.flip();
len = buffer.limit();
byte[] temp = new byte[len];
buffer.get(temp);
recStr = new String(temp);
buffer.compact();
}
buffer.clear(); // 清空buffer
if (count < 0) { // count<0,说明服务端已经断开
socketChannel.close();
}
} catch (Exception e) {
if (socketChannel != null) {
socketChannel.close();
}
e.printStackTrace();
}
socketChannel = null;
return recStr;
}
是不是编码的问题,删掉这句试试