我将数据王byte[]中写入,只读一簇
publicbyte[]saveUrlAs(StringphotoUrl,StringfileName){bytebuffer[]=newbyte[5120];try{U...
public byte[] saveUrlAs(String photoUrl, String fileName)
{
byte buffer[] = new byte[5120];
try
{
URL url = new URL(photoUrl);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
DataInputStream in = new DataInputStream(connection.getInputStream());
DataOutputStream out = new DataOutputStream(new FileOutputStream(fileName));
// for (int count = 0; (count = in.read(buffer)) > 0;)
// out.write(buffer, 0, count);
int leng=in.read(buffer);
while(in.read(buffer)!=-1){
out.write(buffer,0,leng);
leng=in.read(buffer);
}
out.close();
in.close();
//connection.disconnect();
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
return buffer ;
}
就是无论多大,只读5kb,哪位大神给解决下,很急的 展开
{
byte buffer[] = new byte[5120];
try
{
URL url = new URL(photoUrl);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
DataInputStream in = new DataInputStream(connection.getInputStream());
DataOutputStream out = new DataOutputStream(new FileOutputStream(fileName));
// for (int count = 0; (count = in.read(buffer)) > 0;)
// out.write(buffer, 0, count);
int leng=in.read(buffer);
while(in.read(buffer)!=-1){
out.write(buffer,0,leng);
leng=in.read(buffer);
}
out.close();
in.close();
//connection.disconnect();
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
return buffer ;
}
就是无论多大,只读5kb,哪位大神给解决下,很急的 展开
1个回答
2014-03-22
展开全部
final int max=5120;
public byte[] saveUrlAs(String photoUrl, String fileName){
ByteArrayoutputStream bout=new ByteArrayoutputStream();
try{
URL url = new URL(photoUrl);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
DataInputStream in = new DataInputStream(connection.getInputStream());
DataOutputStream out = new DataOutputStream(new FileOutputStream(fileName));
byte buffer[] = new byte[1024];
int len, size=0;
while( (len=in.read(buffer))!=-1){
if(size+len>max){
out.write(buffer, 0, max-size);
bout.write(buffer, 0, max-size);
break;
}else{
out.write(buffer, 0, len);
bout.write(buffer, 0, len);
}
size+=len;
}
out.close();
in.close();
//connection.disconnect();
}catch (Exception e){
e.printStackTrace();
return null;
}
return bout.toByteArray();
}
是这个意思吗。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询