开发者社区> 问答> 正文

编译时没出错,运行时在类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 还不咋会,只能做到这,有错误帮助完成一下,谢谢``

展开
收起
茶什i 2019-07-26 18:09:08 1020 0
1 条回答
写回答
取消 提交回答
  • 主要从事 Java 后端开发。
    采纳回答
    1. public static void main(String[] args) 是程序运行的入口,没有这个你程序跑步起来。
    2. 方法需要调用才能运行。 大概写了一下,因为不是很清楚这段代码要做的事情,运行时会报错,是代码参数的问题,你可以自己调整一下
    public class test {
        public static void main(String[] args) throws Exception {
            DownUtil downUtil = new DownUtil("G:/test", "test.txt",2);
            downUtil.getCompleteRate();
        }
    }
    
    
    2019-07-27 14:12:30
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
15分钟打造你自己的小程序 立即下载
小程序 大世界 立即下载
《15分钟打造你自己的小程序》 立即下载