📄 downpartfile.java
字号:
import java.io.*;
import java.net.*;
/*
* DownPartFile.java
*
* Created on 2006年12月10日, 下午7:52
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author Administrator
*/
public class DownPartFile{
URL url=null;
HttpURLConnection hURL;
String httpURLString;
BufferedInputStream bufferIn;
RandomAccessFile file;
String fileName;
int fileLength;
/** Creates a new instance of DownPartFile */
public DownPartFile(String url,String FilePart) {
httpURLString=url;
fileName=FilePart;
}
//下载
public void down(){
int length=0;
try {
if((fileLength=getSize())!=-1){
System.out.println("文件长度为:"+fileLength);
}
url=new URL(httpURLString);
hURL=(HttpURLConnection) url.openConnection();
//hURL.setRequestProperty("User-Agent","NetFox");
String sProperty="bytes="+0+"-";
hURL.setRequestProperty("RANGE",sProperty);
bufferIn=new BufferedInputStream(hURL.getInputStream());
//file=new FileOutputStream(fileName);
int size;
byte[] buf=new byte[1024];
while((size=bufferIn.read(buf))!=-1){
System.out.println(size);
file.write(buf,0,size);
length+=size;
}
System.out.println("共写入:"+length);
file.close();
bufferIn.close();
hURL.disconnect();
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
//下载指定的段
public void getPartFile(long startPos,long endPos){
int partLength=(int)(endPos-startPos);
System.out.println("文件段长度:"+partLength);
int length=0;
try {
url=new URL(httpURLString);
hURL=(HttpURLConnection) url.openConnection();
String sProperty="bytes="+startPos+"-";
System.out.println("文件开始下载位置:"+sProperty);
hURL.setRequestProperty("RANGE",sProperty);
BufferedInputStream bIn=new BufferedInputStream(hURL.getInputStream());
byte[] buf=new byte[1024];
int len;
file=new RandomAccessFile(fileName,"rw");
file.seek(startPos);
while((len=bIn.read(buf))!=-1){
if(len>partLength-length){
len=partLength-length;
}
file.write(buf,0,len);
System.out.println(len);
length+=len;
if(length>=partLength){
file.close();
break;
}
}
System.out.println("下载文件长度:"+length);
} catch (IOException ex) {
ex.printStackTrace();
}
}
public int getSize(){
int fileLength=-1;
try {
url=new URL(httpURLString);
hURL=(HttpURLConnection) url.openConnection();
int code=hURL.getResponseCode();
if(code==-1){
System.out.println("不是有效的状态码!");
return -1;
}
if(code==400){
System.out.println("发生错误!");
return -1;
}
if(code==200){
System.out.println("正确接收信息!");
}
String sHeader;
for(int i=1;;i++){
//查找标识文件长度的文件头,获取文件长度
sHeader=hURL.getHeaderFieldKey(i);
if(sHeader!=null){
if(sHeader.equals("Content-Length")){
fileLength=
Integer.parseInt(hURL.getHeaderField(sHeader));
break;
}
} else
break;
}
} catch (IOException ex) {
ex.printStackTrace();
}
return fileLength;
}
public static void main(String[] args) {
String urlString=new String("http://www.zhouyunhai.com/book/whomovedmycheese.exe");
String filePart=new String("H:\\2.exe");
DownPartFile downFile=new DownPartFile(urlString,filePart);
//downFile.down();
long startPos=0;
long endPos=10000000;
downFile.getPartFile(startPos,endPos);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -