不会做了,执行出现异常,多线程网络下载程序书中部分url

import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import org.omg.CORBA.portable.InputStream;
public class DownUtil {
public static void main(String[] args) throws Exception {
String path="https://ibaotu.com/sucai/18055535.html?clickFrom=colorsearch&kwd=gif??";

        String target="11111";  

        int num=3;

        DownUtil util=new DownUtil(path,target,num);

            util.download();
    }
        private  String  path;

        private  String  targetFile;

        private  int  threadNum;

        private   DownThread[]  threads;

        private    int   fileSize;

        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-flash,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++)
    { 
          sumSize+=threads[i].Length;

     }

          return  sumSize*1.0/fileSize;
    }

          private  class  DownThread  extends Thread
    {
          private int startPos;

          private  int  currentPartSize;

          private  RandomAccessFile  currentPart;

          public int Length;

          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,"
    +"application/x-shockwave-flash,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");

              InputStream  instream =(InputStream)conn.getInputStream();

              instream.skip(this.startPos);

              byte[] buffer = new  byte[1024];

              int  hasRead=0;

              while(Length<currentPartSize&&(hasRead=instream.read(buffer))!=-1)
    {
              currentPart.write(buffer,0,hasRead);

              Length+=hasRead;
    }
              currentPart.close();

              instream.close();

    }

              catch(IOException e)
    {  
              e.printStackTrace();
    }
    }
    }
        }

编译正常,执行时出现

Exception in thread "main" java.io.IOException: 试图将文件指针移到文件开头之前。
    at java.io.RandomAccessFile.setLength(Native Method)
    at DownUtil.download(DownUtil.java:73)
    at DownUtil.main(DownUtil.java:18)
谢谢解决一下,刚学》

1个回答

file.setLength(fileSize);
调试下,看看你filesize是多少,是不是<0了,或者因为>2GB而溢出
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问