首页

关于java代码实现将各种资源文件通过URL下载到指定路径下载代码示例

标签:FileUrlDownloadUtil,url文件资源解析,HttpURLConnection,URL下载工具类     发布时间:2019-01-26   

一、前言

关于java.net.HttpURLConnection的定义FileUrlDownloadUtil文件资源下载工具类,实现java代码通过URL文件资源地址下载指定将inux和Windows操作系统路径目录下代码示例。

二、代码示例

import java.io.BufferedInputStream;@b@import java.io.File;@b@import java.io.FileOutputStream;@b@import java.io.IOException;@b@import java.io.OutputStream;@b@import java.net.HttpURLConnection;@b@import java.net.MalformedURLException;@b@import java.net.URL;@b@import java.net.URLConnection;@b@ @b@public class  FileUrlDownloadUtil{@b@	@b@	/**@b@	 * 说明:根据指定URL将文件下载到指定目标位置@b@	 * @param urlPath@b@	 *            下载路径@b@	 * @param downloadDir@b@	 *            文件存放目录@b@	 * @return 返回下载文件@b@	 */@b@	@SuppressWarnings("finally")@b@	public static File downloadFile(String urlPath, String downloadDir) {@b@		File file = null;@b@		try {@b@			// 统一资源@b@			URL url = new URL(urlPath);@b@			// 连接类的父类,抽象类@b@			URLConnection urlConnection = url.openConnection();@b@			// http的连接类@b@			HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;@b@			//设置超时@b@			httpURLConnection.setConnectTimeout(1000*5);@b@			//设置请求方式,默认是GET@b@			httpURLConnection.setRequestMethod("POST");@b@			// 设置字符编码@b@			httpURLConnection.setRequestProperty("Charset", "UTF-8");@b@			// 打开到此 URL引用的资源的通信链接(如果尚未建立这样的连接)。@b@			httpURLConnection.connect();@b@			// 文件大小@b@			int fileLength = httpURLConnection.getContentLength();@b@ @b@			// 控制台打印文件大小@b@			System.out.println("您要下载的文件大小为:" + fileLength / (1024 * 1024) + "MB");@b@ @b@			// 建立链接从请求中获取数据@b@			URLConnection con = url.openConnection();@b@			BufferedInputStream bin = new BufferedInputStream(httpURLConnection.getInputStream());@b@			// 指定文件名称(有需求可以自定义)@b@			@b@			String fileFullName="";@b@			if(urlPath.contains("=")){@b@				fileFullName=urlPath.substring(urlPath.lastIndexOf("=")+1);@b@			}@b@			@b@			// 指定存放位置(有需求可以自定义)@b@			String path = downloadDir + File.separatorChar + fileFullName;@b@			file = new File(path);@b@			// 校验文件夹目录是否存在,不存在就创建一个目录@b@			if (!file.getParentFile().exists()) {@b@				file.getParentFile().mkdirs();@b@			}@b@ @b@			OutputStream out = new FileOutputStream(file);@b@			int size = 0;@b@			int len = 0;@b@			byte[] buf = new byte[2048];@b@			while ((size = bin.read(buf)) != -1) {@b@				len += size;@b@				out.write(buf, 0, size);@b@				// 控制台打印文件下载的百分比情况@b@				System.out.println("下载了-------> " + len * 100 / fileLength + "%\n");@b@			}@b@			// 关闭资源@b@			bin.close();@b@			out.close();@b@			System.out.println("文件下载成功!");@b@		} catch (MalformedURLException e) {@b@			// TODO Auto-generated catch block@b@			e.printStackTrace();@b@		} catch (IOException e) {@b@			// TODO Auto-generated catch block@b@			e.printStackTrace();@b@			System.out.println("文件下载失败!");@b@		} finally {@b@			return file;@b@		}@b@ @b@	}@b@ @b@	/**@b@	 * 测试 @b@	 * @param args@b@	 */@b@	public static void main(String[] args) {@b@	@b@		// 指定资源地址,下载文件测试@b@		downloadFile("http://www.xwood.net/xwood-upload/servlet/DownLoadServlet?filename=777777.jpg", "C:/temp/");@b@ @b@	}@b@}