明妍宁 2019-07-27 16:10 采纳率: 62.5%
浏览 407
已采纳

不会做了,执行出现异常,多线程网络下载程序书中部分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条回答 默认 最新

  • threenewbee 2019-07-27 16:44
    关注
    file.setLength(fileSize);
    调试下,看看你filesize是多少,是不是<0了,或者因为>2GB而溢出
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料