远程下载文件

来源:百度文库 编辑:神马文学网 时间:2024/05/01 23:38:01
/*
* 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";
//确定文件是否已经下载,但没有下载完成
public boolean checkfileExist(String filepath)
{
File file = new File(filepath);
if (file.exists())
{
return true;
}
return false;
}
//确定已经下载了的文件大小
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("[文件下载开始]来源="+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))//采用断点续传,这里的依据是看下载文件是否在本地有.tp有扩展名同名文件
{
System.out.println("[断点续传]...");
long fileSize =getFileSize(tpdespath); //取得文件在小,以便确定随机写入的位置
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");//随机方位读取
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("[下载进度=]"+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("[下载进度=]"+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);
}
}