tomcat servlet java.net.SocketTimeoutException: Read timed out 解决办法
2013-01-25 14:30
861 查看
最近在整一个拍照上传,手机通过无线网络上传照片到服务器,服务器端是JAVA写的servlet;思路是将较大的图片分块发送到服务器进行拼装。但发现服务端在读取图片数据时候( InputStream.read)多半是java.net.SocketTimeoutException: Read timed out,网络连接并未超时或断开,服务器的处理结果都发送给手机端(当然是错误消息);而用WIFI这种错误比较少,猜测多半是网络慢引起的数据接收超时。在网上找了好久,好几天都无结果;实在没辙了,只是试试强制读取的办法,在超时的时候还是去读,直到数据读完或者出现网络错误为止。改改程序一试,很OK,成功了!将代码附上:
//total为数据包大小
swapStream = new ByteArrayOutputStream(total);
int len = buff.length;
if(len > total)
len = total;
while (total>0)
{
//int rc = 0;
try
{
rc = inStream.read(buff, 0,len);
if(rc>0)
{
debuginfo.append("read2 " + rc + " " + System.currentTimeMillis() + " ");
swapStream.write(buff, 0, rc);
total -= rc;
len = buff.length;
if(len > total)
len = total;
}
}
catch(java.net.SocketTimeoutException ste)
{
System.out.println(ste.toString());
}
catch(Exception ste)
{
System.out.println(ste.toString());
break;
}
}
//total为数据包大小
swapStream = new ByteArrayOutputStream(total);
int len = buff.length;
if(len > total)
len = total;
while (total>0)
{
//int rc = 0;
try
{
rc = inStream.read(buff, 0,len);
if(rc>0)
{
debuginfo.append("read2 " + rc + " " + System.currentTimeMillis() + " ");
swapStream.write(buff, 0, rc);
total -= rc;
len = buff.length;
if(len > total)
len = total;
}
}
catch(java.net.SocketTimeoutException ste)
{
System.out.println(ste.toString());
}
catch(Exception ste)
{
System.out.println(ste.toString());
break;
}
}
相关文章推荐
- java.net.SocketTimeoutException: Read timed out 的解决办法 android平台 腾讯微博开发
- java.net.SocketTimeoutException: Read timed out 异常解决办法
- java.net.SocketTimeoutException: Read timed out 的解决办法
- java.net.SocketTimeoutException: Read timed out解决办法
- spark redis 中碰到java.net.SocketTimeoutException: Read timed out问题的解决
- java.net.SocketTimeoutException: Read timed out 错误解决
- java.net.SocketTimeoutException: Read timed out 错误解决
- java.net.sockettimeoutexception read timed out
- java.net.SocketTimeoutException: Read timed out
- jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: Read timed out
- java后台接收文件java.net.SocketTimeoutException: Read timed out
- java.net.SocketTimeoutException: Read timed out
- java.net.SocketTimeoutException: Read timed out
- java.net.sockettimeoutexception read timed out 异常
- weblogic 服务不响应java.net.SocketTimeoutException: Read timed out
- 调用webservice的时候报错 java.net.SocketTimeoutException: Read timed out
- java.net.SocketTimeoutException: Read timed out
- java.net.SocketTimeoutException: Read timed out
- 【生产问题直播】java.net.SocketTimeoutException: Read timed out
- HttpClient java.net.SocketTimeoutException: Read timed out!