当前位置: 技术问答>java相关
InputStream的问题,帮帮我。
来源: 互联网 发布时间:2015-03-05
本文导语: 程序如下: int bufsize=1024; byte[] buf=new byte[bufsize]; StringBuffer sb=new StringBuffer(); String result=""; String cgipath="http://myhost/cgi-bin/test.exe"; URL cgiurl=new URL(/tech-qa-java/cgipath/index.html); InputStream is=cgiurl.openStream(); int j=is.r...
程序如下:
int bufsize=1024;
byte[] buf=new byte[bufsize];
StringBuffer sb=new StringBuffer();
String result="";
String cgipath="http://myhost/cgi-bin/test.exe";
URL cgiurl=new URL(/tech-qa-java/cgipath/index.html);
InputStream is=cgiurl.openStream();
int j=is.read(buf,0,bufsize);
while(j!=-1){
sb.append(new String(buf,0,j));
j=is.read(buf,0,j);
}
result=new String(sb);
cgi里的数据流是取出来了,但是问题是中间有掉帧的情况,也就是说,java从CGI流里取的数据,中间会掉一些字节,而且掉得也有点规律,cgi里的流输到文件里是正常的。请帮我分析一下,这段程序有什么毛病,谢谢!
int bufsize=1024;
byte[] buf=new byte[bufsize];
StringBuffer sb=new StringBuffer();
String result="";
String cgipath="http://myhost/cgi-bin/test.exe";
URL cgiurl=new URL(/tech-qa-java/cgipath/index.html);
InputStream is=cgiurl.openStream();
int j=is.read(buf,0,bufsize);
while(j!=-1){
sb.append(new String(buf,0,j));
j=is.read(buf,0,j);
}
result=new String(sb);
cgi里的数据流是取出来了,但是问题是中间有掉帧的情况,也就是说,java从CGI流里取的数据,中间会掉一些字节,而且掉得也有点规律,cgi里的流输到文件里是正常的。请帮我分析一下,这段程序有什么毛病,谢谢!
|
int bufsize=1024;
byte[] buf=new byte[bufsize];
byte[] buf2 = new byte[0];
String result="";
String cgipath="http://myhost/cgi-bin/test.exe";
URL cgiurl=new URL(/tech-qa-java/cgipath/index.html);
InputStream is=cgiurl.openStream();
int j=is.read(buf,0,bufsize);
while(j!=-1){
byte[] buf3 = new byte[buf2.length + j];
System.arraycopy(buf2, 0, buf3, 0, buf2.length);
System.arraycopy(buf, 0, buf3, buf2.length, j);
buf2 = buf3;
j=is.read(buf,0,bufsize);
}
result=new String(buf2);
byte[] buf=new byte[bufsize];
byte[] buf2 = new byte[0];
String result="";
String cgipath="http://myhost/cgi-bin/test.exe";
URL cgiurl=new URL(/tech-qa-java/cgipath/index.html);
InputStream is=cgiurl.openStream();
int j=is.read(buf,0,bufsize);
while(j!=-1){
byte[] buf3 = new byte[buf2.length + j];
System.arraycopy(buf2, 0, buf3, 0, buf2.length);
System.arraycopy(buf, 0, buf3, buf2.length, j);
buf2 = buf3;
j=is.read(buf,0,bufsize);
}
result=new String(buf2);
|
public int read(byte[] b,
int off,
int len)
throws IOException
sb.append will return some value
int off,
int len)
throws IOException
sb.append will return some value
|
make a BufferedStream from the InputStream and work with the BufferedStream.