java 下载网络资源的源码供参考

本文提供了一个使用Java实现的网络资源下载示例程序。该程序通过HttpURLConnection从指定URL下载资源,并保存到本地文件中。文章展示了如何处理异常及优化资源分配。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

现在的连接地址可以替换比较简单的方法进行 网络资源的下载。

 

package com.****.****;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class downFile {

	//	private static String url = "http://daily3gp.com/vids/family_guy_penis_car.3gp";
	private static String url = "http://219.138.125.22/myweb/mp3/CMP3/JH19.MP3";
	public static void main(String[] args) {
		downLoadGoogleIcon(url);
	}

	/**
	 * 依据URL获取文件名当为空时 设置文件名为 default.temp
	 * @param url
	 * @return
	 */
	private static String getFileNameFromUrl(String url){

		if(url == null){
			url = "default.temp";
		}else {
			if(url.lastIndexOf("/") != -1){
				url = url.substring(url.lastIndexOf("/") + 1, url.length());
			}else {
				if(url.lastIndexOf(".") != -1){
					url = url.substring(url.lastIndexOf(".") + 1, url.length());;
				}else {
					url = "default.temp";
				}
			}
		}
		System.out.println("the file name is " + url);
		return url;
	}

	/**
	 * 下載网络资源
	 * @param downFileURL
	 */
	private static void downLoadGoogleIcon(String downFileURL) {

		HttpURLConnection httpConnection = null; // 连接类
		InputStream is = null;   // 输入流  网路部分
		FileOutputStream fos = null;  // 输出流  文件部分
		String icon = downFileURL;
		byte buf[] = null;
		try {
			if (icon == null) {
				throw new Exception("访问的URL不存在,请重试。");
			}
			URL url = new URL(icon);
			// 文件路径
			String path = "D:\\TEMP";
			httpConnection = (HttpURLConnection) url.openConnection();
			is = httpConnection.getInputStream(); // 获取输入流
			File pathFile = new File(path);
			if (!pathFile.exists()) {
				pathFile.mkdirs();
			}
			fos = new FileOutputStream(new File(path + "\\" + getFileNameFromUrl(downFileURL)), true); // 获取输出流
			buf = new byte[1024 * 8];
			int len = 0;
			int count = 0;
			while ((len = is.read(buf)) > 0) {
				count ++;
				fos.write(buf, 0, len);
				if(count == 64){
					count = 0;
					try {
						System.out.println("download is paused!!!!");
						Thread.sleep(1); // 性能优化利于资源分配
					} catch (InterruptedException e) {
					}
				}
			}
			System.out.println("download is completed!!!!");
			httpConnection.disconnect();
			is.close();
			fos.flush();
			fos.close();
			buf = null;
			httpConnection = null;
			is = null;
			fos = null;
		} catch (Exception e) {
			System.out.println("downloading error!!!!");
		} finally { // 记得在此回收资源虽然 繁琐但是影响很大。
			try {
				if (is != null) {
					is.close();
				}
				is = null;
				if (fos != null) {
					fos.close();
				}
				fos = null;
				if (httpConnection != null) {
					httpConnection.disconnect();
					httpConnection = null;
				}
				if (buf != null) {
					buf = null;
				}
			} catch (Exception e2) {
			}
		}
	}
}


上面包含了一些资源优化的一些理解,对大家有帮助,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值