明妍宁 2019-11-09 16:16 采纳率: 62.5%
浏览 864
已结题

length cannot be resolved or not a field咋解决,谢谢(网络传输)

package downutil;
import java.net.URL;
import java.util.RandomAccess;
import java.io.RandomAccessFile;
import java.io.Serializable;
import java.net.HttpURLConnection;

public class DownUtil {
private DownThread[] threads;
private String path;
private String targetFile;
private int threadNum;

private int fileSize;
private long startPos;
private int currentPartSize;
private RandomAccessFile currentPart;






public DownUtil(String path,String targetFile,int threadNum) {
    this.path=path;
    this.threadNum=threadNum;
     threads = new DownThread[threadNum];
    this.targetFile=targetFile;
}                                                                                  


public void download()throws Exception{
    URL url=new URL(path);
    HttpURLConnection conn=(HttpURLConnection)url.openConnection();
    conn.setConnectTimeout(5*1000);
    conn.setRequestMethod("GET");
    conn.setRequestProperty(
            "Accept",
            "image/gif,image/jpeg,image/pjpeg,image/pjpeg,"
            +"application/x-shockwave-falsh,application/xaml+xml,"
                    +"application/vnd.ms-xpsdocument,application/x-ms-xbap,"
            +"application/x-ms-application,application/vnd.ms-excel,"
                    +
            "application/vnd.ms-powerpoint,application/msword,*/*");
    conn.setRequestProperty("Accept-Language", "zh-CN");;
    conn.setRequestProperty("Charset", "UTF-8");
    conn.setRequestProperty("Connection","Keep-Alive");

    fileSize=conn.getContentLength();
    conn.disconnect();
    int currentPartSize=fileSize/threadNum+1;
    RandomAccessFile file=new RandomAccessFile(targetFile,"rw");
    file.setLength(fileSize);;
    file.close();
    for(int i=0;i<threadNum;i++) {
        int startPos=i*currentPartSize;
        RandomAccessFile currentPart=new RandomAccessFile(targetFile,"rw");
        currentPart.seek(startPos);;



        threads[i]= new DownThread(startPos, currentPartSize,currentPart);
         threads[i].start();
    }
}


    public double getCompleteRate() {
        int sumSize=0;

        for(int i=0;i<threadNum;i++){
//          此处显示length cannot be resolved or not a field?
            sumSize+=threads[i].length;
        }

        return sumSize*1.0/fileSize;
    }

    public class DownThread extends Thread{

        private int startPos;
        private int currentPartSize;
        private RandomAccessFile currentPart;


        private int[] threadNum;
        public DownThread(int startPos, int currentPartSize,RandomAccessFile currentPart) {
            this.startPos=startPos;
            this.currentPartSize=currentPartSize;
            this.currentPart= currentPart;
                }



    }

    public void run() {
        try {
            URL url=new URL(path);
            HttpURLConnection conn=(HttpURLConnection)url.openConnection();
            conn.setConnectTimeout(5*1000);
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept",
                    "image/gif,image/jpeg,image/pjpeg,image/pjpeg,"
                    +"applicaton/x-shockwave-flash,application/xaml+xml,"
                            +"spplication/vnd.ms-xpsdocument,application/x-ms-xbap,"
                    +"application/x-ms-application,application/vnd.ms-excel,"
                            +"appplication/vnd.ms-powerpoint,application/mswerd,*/*");
            conn.setRequestProperty("Accept-Language", "zh-CN");
            conn.setRequestProperty("Charset", "UTF-8");
            java.io.InputStream inStream=conn.getInputStream();
            inStream.skip(this.startPos);
            byte[] buffer=new byte[1024];
            int hasRead=0;
            int length;
            while(length<currentPartSize&&(hasRead=inStream.read(buffer))!=-1);
            {
                currentPart.write(buffer,0,hasRead);
                length+=hasRead;
                currentPart.close();
                inStream.close();
            }                           
        }
            catch(Exception e) {
                e.printStackTrace();
            }

        }
             public  class MultiThreadDown{

public static void main(String[] args)throws Exception{



         final DownUtil downUtil=new DownUtil("http://127.0.0.1/"+
        "/ProgramData/网页/摄影/0001.jpg",
        "ios.jpg",4);
            downUtil.download();
            new Thread(()->{
            while( downUtil.getCompleteRate()<1){
                System.out.println("已完成:"+downUtil.getCompleteRate());
                try {
                    Thread.sleep(1000);}
                    catch(Exception ex) {}
                }
            }).start();

        }       

}

  • 写回答

2条回答 默认 最新

  • threenewbee 2019-11-09 17:46
    关注

    DownThread没有叫做length的属性,是不是写错了,或者private int length;,是私有的。

    评论

报告相同问题?

悬赏问题

  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组