明妍宁 2019-07-26 13:04 采纳率: 62.5%
浏览 257
已采纳

编译时没出错,运行时在类DownUtil中找不到主方法

`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 {

    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();
}
}
}
    }

            这是一本书中的代码,只有中间一部分,头没有,运行时找不到主方法
            显示请将主方法定义为public static void main(String[] args),
            刚学使用eclipse 还不咋会,只能做到这,有错误帮助完成一下,谢谢``




  • 写回答

2条回答

  • 毕小宝 博客专家认证 2019-07-26 13:34
    关注

    这只是一个普通的类,没有定义 main 方法,你需要自己定义一个 main 方法,可以引用 DownUtil 的方法完成一些操作,例如:

    public static void main(String[] args) {
        String  path = "";//路径,自己定义
            String target = "";//自己定义
             int num = 3;
        DownUtil util = new DownUtil(path,target,num);
            util.download();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备