import java.io.*;
import java.net.*;
import java.util.*;
public class UrlDownload {
public final static boolean DEBUG = true;//是否输出调试信息
private static int BUFFER_SIZE = 8096;//缓冲区大小
private Vector vDownLoad = new Vector();//URL列表
private Vector vFileList = new Vector();//下载后的保存文件名列表
/**
* 构造方法
*/
public UrlDownload() {
}
/**
* 清除下载列表
*/
public void resetList() {
vDownLoad.clear();
vFileList.clear();
}
/**
* 增加下载列表项
*
* @param url String
* @param filename String
*/
public void addItem(String url, String filename) {
vDownLoad.add(url);
vFileList.add(filename);
}
/**
* 根据列表下载资源
*/
public void downLoadByList() {
String url = null;
String filename = null;
//按列表顺序保存资源
for (int i = 0; i < vDownLoad.size(); i++) {
url = (String) vDownLoad.get(i);
filename = (String) vFileList.get(i);
try {
saveToFile(url, filename);
} catch (IOException err) {
if (DEBUG) {
System.out.println("资源[" + url + "]下载失败!!!");
}
}
}
if (DEBUG) {
System.out.println("下载完成!!!");
}
}
/**
* 将HTTP资源另存为文件
*
* @param destUrl String
* @param fileName String
* @throws Exception
*/
public void saveToFile(String destUrl, String fileName) throws IOException {
FileOutputStream fos = null;
BufferedInputStream bis = null;
HttpURLConnection httpUrl = null;
URL url = null;
byte[] buf = new byte[BUFFER_SIZE];
int size = 0;
//建立链接
url = new URL(destUrl);
httpUrl = (HttpURLConnection) url.openConnection();
//连接指定的资源
httpUrl.connect();
//获取网络输入流
bis = new BufferedInputStream(httpUrl.getInputStream());
//建立文件
File file = new File(fileName);
if(!file.exists()) {
if(!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
file.createNewFile();
}
fos = new FileOutputStream(fileName);
if (this.DEBUG)
System.out.println("正在获取链接[" + destUrl + "]的内容...\n将其保存为文件["
+ fileName + "]");
//保存文件
while ((size = bis.read(buf)) != -1)
fos.write(buf, 0, size);
fos.close();
bis.close();
httpUrl.disconnect();
}
/**
* 设置代理服务器
* @param proxy String
* @param proxyPort String
*/
public void setProxyServer(String proxy, String proxyPort) {
System.getProperties().put("proxySet", "true");
System.getProperties().put("proxyHost", proxy);
System.getProperties().put("proxyPort", proxyPort);
}
/**
* 设置认证用户名与密码
* @param uid String
* @param pwd String
*/
public void setAuthenticator(String uid, String pwd) {
//Authenticator.setDefault(new MyAuthenticator(uid, pwd));
}
/**
* 主方法(用于测试)
* @param argv String[]
* @throws IOException
*/
public static void main(String argv[]) throws IOException {
UrlDownload oInstance = new UrlDownload();
oInstance.addItem("http://192.168.1.2/demos/1.zip", "c:/1.zip");
oInstance.downLoadByList();
}
}
分享到:
相关推荐
JAVA 根据Url 接口 获取文件名称和类型,亲测可用。输入参数地址即可。
* 使用URLConnection下载文件或图片并保存到本地。 * * @author 老紫竹(laozizhu.com) */ public class URLConnectionDownloader { public static void main(String[] args) throws Exception { ...
jsoup是一个Java HTML Parser。能够从URL、文件或字符串解析HTML。利用DOM traversal或CSS selectors查找和抽取数据。能够操作HTML元素,属性和文本。能够依据一个白名单过滤用户提交的内容。
第一:创建HttpURLConnection 第二:打开URL,创建一个InputStream 第三:逐行(逐字节)读取,如果需要,转换编码,放入字符串。 好,一下就开始代码吧: 代码如下:public String getUrlContent(String path){ ...
HttpCilent 跟 HttpURLConnection 是安卓原生的用来实现http请求的类: Android 6.0之后取消了HttpClient,不支持跟新 ,今天小编使用的是HttpURLConnection : ... //你要下载文件的路径 String urlPa
代码如下:/** * 从指定的URL中获取数组 * @param urlPath * @return * @throws Exception */ public static String readParse(String urlPath) throws Exception { ByteArrayOutputStream outStream = new ...
android 中通过HttpURLConnection获取服务器上的html文件,和图片文件,并保持到本地
* 从网络Url中下载文件 * * @param urlStr * @param fileName * @param savePath * @throws IOException */ public static void downLoadByUrl(String urlStr, String fileName, String savePath) throws ...
Android 上传文件至服务器和下载文件至本地,亲测有效,只需传入相关参数即可。/** * android上传文件到服务器 * * @param file * 需要上传的文件 * @param RequestURL * 请求的rul * @return 返回...
FileUtil提供获取文件后缀名、修改文件的后缀名、删除文件或目录、根据流判断流的编码格式、获取文件的编码格式、创建文件、获得文件大小、创建文件目录 方法 OpenOfficeUtil提供使用openoffice把office文件转换成...
您可以添加文本字段,文件系统中的文件或任何InputStream中的文件。 使用简单: //Make our connectionURL url = new URL("http://httpbin.org/post");connection = (HttpURLConnection) url.openConnection();//...
8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过网络播放MP3——Runnable存储FileOutputStream技巧 8.9 设置远程下载音乐为...
本文实例讲述了Golang+Android基于HttpURLConnection实现的文件上传功能。分享给大家供大家参考,具体如下: 这里要演示的是使用Android程序作为客户端(使用HttpURLConnection访问网络),Golang程序作为服务器端,...
本文实例讲述了JSP实现远程文件下载保存到服务器指定目录中的方法。分享给大家供大家参考,具体如下: <%@page import=java.net.*,java.io.*%> <%! public boolean saveUrlAs(String photoUrl, ...
前面整理过基于UDP/TCP协议的网络编程,这里教大家用简单的代码实现下载网络资源的功能。在这里使用到了http协议的相关java类---URL和HttpURLConnection,关于这两个类的深入学习可以查看相关API。
– 创建一个HttpURLConnection对象 : HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); – 获取一个InputStream对象 : urlConn.getInputStream() – 访问网络的权限 : android.permission....
本文实例讲述了Android编程使用HTTP协议与TCP协议实现上传文件的方法。分享给大家供大家参考,具体如下: ...1. 通过URL封装路径打开一个HttpURLConnection 2.设置请求方式以及头字段:Content-Type、Content
java URLConnection 的详细介绍,牛人项目经验的总结,绝对通俗易懂,包括cookie操作,上传文件,以及下载网页等介绍,是非常难得文档和教材
HttpURLConnection conn = (HttpURLConnection)url.openConnection(); //设置请求超时与请求方式 conn.setReadTimeout(5*1000); conn.setRequestMethod("GET"); //从链接中获取一个输入流对象 ...
分享给大家供大家参考,具体如下: /** * * @param aUrl 网址 * @param aEncode 编码 ...* @throws Exception 对外抛出异常 */ public String getHTML... HttpURLConnection conn = (HttpURLConnection) url.openCon