/*
* Created on 2007-4-10
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.dreamwin.util;
import java.io.*;
import java.net.*;
/**
* 作者:謝榮華 日期:2007-4-10 功能:功能描敘
*
*/
public class GetFileFromHTTP
{
private long totalSize=0l;
private final String tempfix="tp";
//確定文件是否已經(jīng)下載,但沒(méi)有下載完成
public boolean checkfileExist(String filepath)
{
File file = new File(filepath);
if (file.exists())
{
return true;
}
return false;
}
//確定已經(jīng)下載了的文件大小
public long getFileSize(String filepath)
{
File file = new File(filepath);
return file.length();
}
public String getTpFileName(String filepath)
{
int index=filepath.lastIndexOf(".");
if(index!=-1)
{
return filepath.substring(0,index)+"."+tempfix;
}
return filepath;
}
private void FileRename(String fName,String nName)
{
File file = new File(fName);
file.renameTo(new File(nName));
file.delete();
}
//獲取文件流
public boolean getFile(String srcpath,String despath)
{
String tpdespath=getTpFileName(despath);
System.out.println("[文件下載開(kāi)始]來(lái)源="+srcpath);
try
{
URL l_url = new java.net.URL(srcpath);
HttpURLConnection l_connection =(HttpURLConnection)l_url.openConnection();
l_connection.connect();
totalSize=Long.parseLong(l_connection.getHeaderField("Content-Length"));
System.out.println("[文件大小]="+totalSize);
int len=0;
int totallen=0;
byte[] bt = new byte[1024];
if (l_connection.getResponseCode() == HttpURLConnection.HTTP_OK)
{
if(checkfileExist(tpdespath))//采用斷點(diǎn)續(xù)傳,這里的依據(jù)是看下載文件是否在本地有.tp有擴(kuò)展名同名文件
{
System.out.println("[斷點(diǎn)續(xù)傳]...");
long fileSize =getFileSize(tpdespath); //取得文件在小,,以便確定隨機(jī)寫入的位置
System.out.println("[已有大小]="+fileSize);
l_connection.disconnect();
l_connection =(HttpURLConnection)l_url.openConnection();
l_connection.setRequestProperty("RANGE", "bytes="+fileSize+"-");
RandomAccessFile raFile=new RandomAccessFile(tpdespath,"rw");//隨機(jī)方位讀取
raFile.seek(fileSize);
BufferedInputStream bis = new BufferedInputStream(l_connection.getInputStream());
while ((len = bis.read(bt)) > 0)
{
totallen=totallen+len;
raFile.write(bt,0,len);
long progress=((totallen*100)/totalSize);
System.out.println("[下載進(jìn)度=]"+progress+"%");
}
raFile.close();
bis.close();
}
else
{
System.out.println("[新建傳輸]...");
FileOutputStream fos = new FileOutputStream(tpdespath);
DataOutputStream dos = new DataOutputStream(fos);
BufferedInputStream bis = new BufferedInputStream(l_connection.getInputStream());
while ((len=bis.read(bt)) > 0)
{
dos.write(bt,0,len);
totallen=totallen+len;
long progress=((totallen*100)/totalSize);
System.out.println("[下載進(jìn)度=]"+progress+"%");
}
bis.close();
dos.close();
fos.close();
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
if(totalSize==getFileSize(getTpFileName(despath)))
{
FileRename(getTpFileName(despath),despath);
System.out.println("[文件下載完成]保存="+despath);
}
return true;
}
public static void main(String args[])
{
GetFileFromHTTP gf=new GetFileFromHTTP();
String srcpath="http://59.38.125.122/uploadfile/vodone/97d36f6cf62208bcf8ce21c065355d48/97d36f6cf62208bcf8ce21c065355d48.wmv";
String despath="E://01.wmv";
gf.getFile(srcpath,despath);
}
}