URL url = new URL(pic);
HttpURLConnection httpconn = (HttpURLConnection) url.openConnection();
httpconn.connect();
Gether.jLabel_Down.setText("正在下载 "+time+"...");
System.out.println("服务器返回"+httpconn.getContentLength());
ds = new DataInputStream(httpconn.getInputStream());
out = new DataOutputStream(new FileOutputStream(file));
int size = 0,j = 0;
System.out.println("得到"+ds.available());
while ((size = ds.read()) != -1) {
out.write(size);
//j = j + size;
//System.out.println(ds.read());
//show(j,httpconn);
}请问怎样每次读到了多少个字节呢?还有输出结果是
服务器返回256225
得到2631

解决方案 »

  1.   

    读到了ds.available()个字节...... 
      

  2.   

    size   =   ds.read(), size就是每次读的大小。 你要用循环来读httpconn.getContentLength()大小的内容。
    int offset = 0;
    contentLength = httpconn.getContentLength();
    byte data[] = data = new byte[4096];
    while (offset < contentLength) {
                if (contentLength - offset >= data.length) {
                    byteRead = ds.read(data, 0, data.length);
                } else {
                    byteRead = ds.read(data, 0, contentLength - offset);
                }
                if (byteRead == -1) {
                    //error
                    ds.close();
                    out.close();
                }
                out.write(data, 0, byteRead);
                offset+= byteRead;
            }
            ds.close();
            out.close();
      

  3.   

    看看jdk5.0里对available()方法的说明你就明白我说什么了~~
      

  4.   

    在问题个问题,JAVA 能设置文件属性吗,比如只读,隐藏
      

  5.   

    怎么感觉代码有些问题.            
     URL url = new URL(pic); 
    HttpURLConnection httpconn = (HttpURLConnection) url.openConnection(); 
    httpconn.connect(); 
    Gether.jLabel_Down.setText("正在下载 "+time+"..."); 
    System.out.println("服务器返回"+httpconn.getContentLength()); 
    ds = new DataInputStream(httpconn.getInputStream()); 
    out = new DataOutputStream(new FileOutputStream(file));
    byte[] b=new byte[500];//应该有定义每次写多少内容,大小你也根据原内容ds来定义
    int size = 0,j = 0; 
    System.out.println("得到"+ds.available()); 
    while ((size = ds.read(b)) != -1) { 
    out.write(b);//写入的内容
     
    //j = j + size; 
    //System.out.println(ds.read()); 
    //show(j,httpconn);