`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 还不咋会,只能做到这,有错误帮助完成一下,谢谢``
public class test {
public static void main(String[] args) throws Exception {
DownUtil downUtil = new DownUtil("G:/test", "test.txt",2);
downUtil.getCompleteRate();
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。