下载网络资源方法-java版
来源:互联网 发布:informix windows下载 编辑:程序博客网 时间:2024/05/21 06:49
有时需要用到程序下载某个网络资源,比如图片,word文档,pdf,web页面等,网上也有很多,这里用到重写一下,方便热后直接使用,懒人计划.... : )
直接看看方法
public class Funs {public static String downloadNetFile(String refFileURL, String refSavePath, String refFileType){/* parameters: * 1,refFileURL: is a url of file which exists in international (www)net; * 2,refSavePath: is path of file which success download and save in it, eg.: e:\path; * 3,refFileType: is a type of file, eg.: gif or jpg or png or htm or html or ppt or doc or... * * return type: * data type: String, only return a file name or a empty String, not include path * */String retValue = "";if(null == refFileURL || refFileURL.trim().isEmpty() || null == refSavePath || refSavePath.trim().isEmpty()|| null == refFileType || refFileType.trim().isEmpty()){retValue = "";}else{//default value of fileNameString fileName = "down_" + new SimpleDateFormat("yyyyMMdd_HHmmssSSS").format(Calendar.getInstance().getTime());String fileType = refFileType.trim();if(!fileType.startsWith(".")){fileType = "." + fileType;}fileName = fileName + fileType;String savePath = refSavePath.trim();if(!savePath.endsWith("\\")){savePath = savePath + "\\";}String saveDownFile = savePath + fileName; String fileURL = refFileURL.trim();try {//create URL object URL fileUrl = new URL(fileURL);//connect to net URL resource and create HttpURLConnection object HttpURLConnection connection = (HttpURLConnection) fileUrl.openConnection(); //also can to use BufferedInputStream and BufferedOutputStream object //get net source input stream DataInputStream ins = new DataInputStream(connection.getInputStream()); //rewrite the new file content if exists same file DataOutputStream out = new DataOutputStream(new FileOutputStream(saveDownFile,false)); byte[] buffer = new byte[4096]; int count = 0; while ((count = ins.read(buffer)) > 0){ out.write(buffer, 0, count); } out.close(); //close dataInputStream and release resource ins.close(); //close dataOutputStream and release resource connection.disconnect(); //close net download stream retValue = fileName;} catch (Exception e) {retValue = "";}savePath = null;fileURL = null;fileType = null;}return retValue;}}
0 0
- 下载网络资源方法-java版
- Java多线程下载网络资源
- 网络资源下载
- 网络资源下载
- 网络资源 下载
- java 下载网络资源的源码供参考
- 登录下载网络资源之Java代码
- java多线程下载网络资源(支持断点续传)
- nodejs下载网络资源的三种方法写入数据
- Java网络资源
- java 网络资源
- java jsp+servlet文件上传下载、本地及网络资源的下载
- 嵌入式网络资源下载地址
- 网络资源的下载安装
- 多线程下载网络资源
- URL网络资源下载
- 常用网络资源下载地址
- 简单的网络资源下载
- 接收不明长度字符串问题
- 嵌入式系统视频信号接收、显示 与存储程序设计与实现
- 调用标签
- 十八周 acm--1
- 接收不明长度字符串问题
- 下载网络资源方法-java版
- JAVA的各种O (PO、VO、BO......)
- 'weblogic.kernel.Default (self-tuning) 问题weblogic层面解决办法
- 统计分词/无字典分词学习(1):介绍和语料准备
- Java基本类型与C++基本类型的一些比较
- 程序员 你幸福吗
- 修改shell中的字体颜色
- 跨平台 C/C++ memcached 客户端 memcacheclient 介绍
- linux openssl简单的介绍与实例讲解