如何使用超时读取HttpServletRequest的数据
1个回答
展开全部
1.你可以自己“从紧”的阅读从流(小的bufferSize值,例如8个字节,而不是的readLine和迭代中assertions你。 除此之外,你不能做太多,当你正在阻塞的IO(阻塞在下面的例子中in.read调用)。当一个线程被阻塞在IO也不会反应中断。long timeout = 30000l; //30s
int bufferSize = 8;
ByteArrayOutputStream out = new ByteArrayOutputStream(bufferSize);
try {
long start = System.currentTimeMillis();
int byteCount = 0;
byte[] buffer = new byte[bufferSize];
int bytesRead = -1;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
byteCount += bytesRead;
if (System.currentTimeMillis() > start + timeout) {
//timed out: get out or throw exception or ....
}
}
out.flush();
return byteCount;
} ... catch ... finally ....
int bufferSize = 8;
ByteArrayOutputStream out = new ByteArrayOutputStream(bufferSize);
try {
long start = System.currentTimeMillis();
int byteCount = 0;
byte[] buffer = new byte[bufferSize];
int bytesRead = -1;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
byteCount += bytesRead;
if (System.currentTimeMillis() > start + timeout) {
//timed out: get out or throw exception or ....
}
}
out.flush();
return byteCount;
} ... catch ... finally ....
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询