跪请各位高手请进
下面函数请求一HTTP服务器(服务端读写数据正常),请求下载某一文件的某段数据,结果在调用时有时正常,有时堵塞。请教各位大侠是怎么回事啊
public byte[] download(long start, long len) throws Exception {
long saveSize = 0l;
URL url = null;
URLConnection httpCon = null;
String strUrl = "http://xxx/"+ "servlet/remotefile?fileName=" + java.net.URLEncoder.encode(fileName) + "&actionName=download¶m1=" + start + "," + len;
url = new URL(strUrl);
//连接
httpCon = url.openConnection();
httpCon.setDoInput(true);
httpCon.setUseCaches(false);
httpCon.connect();
System.out.println("start to read data");
java.io.InputStream in = null;
try {
in = httpCon.getInputStream();
}
catch (Exception e) {
}
if (in == null)
throw new Exception("连接网站失败,请与管理员联系!");
java.io.ByteArrayOutputStream os = new java.io.ByteArrayOutputStream();
java.io.DataInputStream di = new java.io.DataInputStream(new java.io.BufferedInputStream(in));
while (in.available() <=0) {
Thread.sleep(10);
}
System.out.println("download start read start="+start + " len="+len);
while(true) {
byte [] b = new byte[1024];//堵塞处,此语句会导致IOException 异常
int n = di.read(b);
if(n<1)
break;
os.write(b,0,n);
}
System.out.println("download end read start="+start + " len="+len);
di.close();
return os.toByteArray();
}