📄 sitefilefetch.java
字号:
/*
* Created on 2005/05/11
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.io.*;
import java.net.*;
import java.util.*;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class SiteFileFetch extends Thread {
SiteInfoBean siteInfoBean;
long[] nPos;
long[] nStartPos;
long[] nEndPos;
FileSplitterFetch[] fileSplitterFetch;
long nFileLength;
boolean bFirst = false;
boolean bStop = false;
File tmpFile;
DataOutputStream output;
public SiteFileFetch(SiteInfoBean bean) throws IOException{
siteInfoBean=bean;
tmpFile = new File(bean.getSFilePath()+File.separator+bean.getSFileName()+".info");
if(tmpFile.exists()){
bFirst = false;
read_nPos();
}
else{
nStartPos = new long[bean.getNSplitter()];
nEndPos = new long[bean.getNSplitter()];
}
}
public void run(){
try{
if(true){
nFileLength = getFileSize();
if(nFileLength == -1){
System.err.print("File Length is not knoen!");
}
else
if(nFileLength == -2){
System.err.print("File is not access");
}
else{
for(int i=0;i<nStartPos.length;i++){
nStartPos[i]=(long)(i*(nFileLength/nStartPos.length));
}
for(int i=0;i<nEndPos.length-1;i++){
nEndPos[i]=nStartPos[i+1];
}
}
fileSplitterFetch = new FileSplitterFetch[nStartPos.length];
for(int i=0;i<nStartPos.length;i++){
fileSplitterFetch[i] = new FileSplitterFetch(siteInfoBean.getSSiteURL(),SiteInfoBean.getSFilePath()
+File.separator + siteInfoBean.getSFileName(),nStartPos[i],nEndPos[i],i);
Utility2.log("Thread"+i+",nStartPos="+nStartPos[i]+",nEndPos="+nEndPos[i]);
fileSplitterFetch[i].start();
}
boolean breakWhile = false;
while(!bStop){
write_nPos();
Utility2.sleep(500);
breakWhile = true;
for(int i=0;i<nStartPos.length;i++){
if(!fileSplitterFetch[i].bDownOver){
breakWhile = false;
break;
}
}
if(breakWhile)
break;
}
System.out.println("file over!");
}
}
catch(Exception e){
e.printStackTrace();
}
}
public long getFileSize(){
int nFileLength = -1;
try{
URL url = new URL(siteInfoBean.getSSiteURL());
HttpURLConnection httpConnection = (HttpURLConnection)url.openConnection();
httpConnection.setRequestProperty("User-Agent","NetFox");
httpConnection.setRequestProperty("GET","HTTP/1.1");
httpConnection.setRequestProperty("User-Agent","jpg");
httpConnection.setRequestProperty("Accept","*/*");
httpConnection.setRequestProperty("Accept-Language","zh-cn");
httpConnection.setRequestProperty("Accept-Encoding","gzip,defate");
httpConnection.setRequestProperty("Connection","Keep-Alive");
httpConnection.setRequestProperty("Content-Length","115");
// int responseCode=httpConnection.getResponseCode();
// if (responseCode >= 400) {
// processErrorCode(responseCode);
// return -2;
// }
String sHeader;
Map map=httpConnection.getHeaderFields();
System.out.print(map.size());
Set set=map.keySet();
Iterator it=set.iterator();
while (it.hasNext())
{
Object key=it.next();
if (((String)key).equalsIgnoreCase("Content-Length"))
{
nFileLength=Integer.parseInt(httpConnection.getHeaderField((String)key));
System.out.println("length = "+nFileLength);
return nFileLength;
}
System.out.println(key+":"+map.get(key)+":"+httpConnection.getHeaderField((String)key));
}
}
catch(Exception e)
{
e.printStackTrace();
}
Utility2.log(nFileLength);
return nFileLength;
}
private void write_nPos(){
try{
output = new DataOutputStream(new FileOutputStream(tmpFile));
output.writeInt(nStartPos.length);
for(int i=0;i<nStartPos.length;i++){
output.writeLong(fileSplitterFetch[i].nStartPos);
output.writeLong(fileSplitterFetch[i].nEndPos);
}
output.close();
}
catch(IOException e){e.printStackTrace();}
catch(Exception e){e.printStackTrace();}
}
private void read_nPos(){
try{
DataInputStream input = new DataInputStream(new FileInputStream(tmpFile));
int nCount = input.readInt();
nStartPos = new long[nCount];
nEndPos = new long[nCount];
for(int i=0;i<nStartPos.length;i++){
nStartPos[i]=input.readLong();
nEndPos[i]=input.readLong();
}
input.close();
}
catch(IOException e){e.printStackTrace();}
catch(Exception e){e.printStackTrace();}
}
private void processErrorCode(int nErrorCode){
System.err.println("Error Code:"+nErrorCode);
}
public void siteStop(){
bStop = true;
for(int i=0;i<nStartPos.length;i++)
fileSplitterFetch[i].splitterStop();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -