import java.io.IOException;
import java.io InputStream;
import java.io RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
```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.setConnectionTimeout(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.setRequesProperty("Charset","UTF-8");
InputStream instream = 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(Exception e)
{
e.printStackTrace();
}
}
}
}
编译时显示:
d:\documents>java -d . DownUtil.java
DownUtil:java:130错误:找不到符号
conn.setConnectionTimeout(5*1000);
符号:方法setConnectionTimeout(int)
位置:类型为HttpURLConnection的变量conn
DownUtil:java:144错误:找不到符号
conn.setRequestProperty("Charser","UTF-8");
符号:方法setRequestProperty(String,String);
位置:类型为HttpURLConnection的变量conn
DownUtil:java:148错误:找不到符号
inStream.skip(this.startPos);
符号:类inStream
位置:类DownUtil.DownThread
DownUtil:java:154错误:找不到符号
while(Length<currentPartSize&&(hasread=inStream.read(buffer))!=-1)
符号:类inStream
位置:类DownUtil.DownThread
DownUtil:java:162错误:找不到符号
inStream.close();
符号:类inStream
位置:类DownUtil.DownThread
5个错误