利用JAVA下载网络文件

package tom;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;

public class DownLoadFile {
	/**
	 * 下载网络文件
	 * @param surl     要下载文件的URL地址
	 * @param fileName 保存下载后文件的文件名
	 * @param filePath 保存的路径
	 */
	public  static void downLoadFile(String surl,String fileName,String filePath)
	{
		String sURL = surl;
		int nStartPos = 0;
		int nRead = 0;
		String sName = fileName;
		String sPath = filePath;
		File dir=new File(filePath);
		if(!dir.exists())
		{
			dir.mkdirs();
		}
		try {
			File file=new File(dir,fileName);
			if(!file.exists())
			{
				file.createNewFile();
			}
			URL url = new URL(sURL);
			// 打开连接
			HttpURLConnection httpConnection = (HttpURLConnection) url
					.openConnection();
			// 获得文件长度
			long nEndPos = getFileSize(sURL);
			RandomAccessFile oSavedFile = new RandomAccessFile(sPath + "\\"
					+ sName, "rw");
			httpConnection
					.setRequestProperty("User-Agent", "Internet Explorer");
			String sProperty = "bytes=" + nStartPos + "-";
			httpConnection.setRequestProperty("RANGE", sProperty);
			InputStream input = httpConnection.getInputStream();
			byte[] b = new byte[1024];
			// 读取网络文件,写入指定的文件中
			while ((nRead = input.read(b, 0, 1024)) > 0 && nStartPos < nEndPos) {
				oSavedFile.write(b, 0, nRead);
				nStartPos += nRead;
			}
			httpConnection.disconnect();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("downLoadFile sucessful !");
	}
	/**
	 * 获取文件长度
	 * @param sURL   下载文件的URL地址
	 * @return   下载文件的大小
	 */
	public static long getFileSize(String sURL) {
		int nFileLength = -1;
		try {
			URL url = new URL(sURL);
			HttpURLConnection httpConnection = (HttpURLConnection) url
					.openConnection();
			httpConnection
					.setRequestProperty("User-Agent", "Internet Explorer");

			int responseCode = httpConnection.getResponseCode();
			if (responseCode >= 400) {
				System.err.println("Error Code : " + responseCode);
				return -2; 
			}
			String sHeader;
			for (int i = 1;; i++) {
				sHeader = httpConnection.getHeaderFieldKey(i);
				if (sHeader != null) {
					if (sHeader.equals("Content-Length")) {
						nFileLength = Integer.parseInt(httpConnection
								.getHeaderField(sHeader));
						break;
					}
				} else
					break;
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(nFileLength);
		return nFileLength;
	}
}

测试文件:

package tom;

import tom.DownLoadFile;

public class DownLoadFile1 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		DownLoadFile.downLoadFile("http://f.hiphotos.baidu.com/image/pic/item/472309f79052982257d81bc2d5ca7bcb0b46d4c3.jpg",
								  "beatifulGril.jpg",
								  "e:\\downloadFile\\");
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值