编译时没出错,运行时在类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个回答

这只是一个普通的类,没有定义 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();
}

这只是一个普通的类,没有定义 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();
}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问