通过httpclient3实现文件下载以及获取文件下载名称
package httpclient3test; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HeaderElement;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod; /**
* @author yan
* @date 2018-3-22 13:39:22
* @version V1.0
* @desc
*/
public class Httpclient3test { public static void main(String[] args) throws IOException {
HttpClient httpClient = new HttpClient();
GetMethod getMethod = new GetMethod("http://android.myapp.com/android/down.jsp?appid=48157&lmid=2031&g_f=-1&actiondetail=0&softname=&downtype=1&enginekeywd=&topicid=-1&pkgid=-1"); int statusCode = httpClient.executeMethod(getMethod);
System.out.println(statusCode); //获取response的返回头信息
Header contentHead = getMethod.getResponseHeader("Content-Disposition"); HeaderElement[] elements = contentHead.getElements();
String filename = null; for (HeaderElement el : elements) {
//遍历,获取filename
NameValuePair pair = el.getParameterByName("filename");
filename = pair.getValue(); if (null != filename) {
break;
}
} System.out.println("filename:" + filename); InputStream is = getMethod.getResponseBodyAsStream(); inputStream2File(is, new File("G:\\tmp\\"+filename)); getMethod.releaseConnection(); } public static void inputStream2File(InputStream is, File file) {
OutputStream os = null; try {
os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = is.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Httpclient3test.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Httpclient3test.class.getName()).log(Level.SEVERE, null, ex);
} finally { try {
if (null != os) {
os.close();
}
} catch (IOException ex) {
Logger.getLogger(Httpclient3test.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (null != is) {
try {
is.close();
} catch (IOException ex) {
Logger.getLogger(Httpclient3test.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
} }
依赖jar包
commons-codec-1.2.jar
commons-httpclient-3.1.jar
commons-logging-1.2.jar
通过httpclient3实现文件下载以及获取文件下载名称的更多相关文章
- C#开发BIMFACE系列10 服务端API之获取文件下载链接
系列目录 [已更新最新开发文章,点击查看详细] 通过BIMFACE控制台或者调用服务接口上传文件成功后,默认场景下需要下载该源文件,下载文件一般需要知道文件的下载链接即可.BIMACE平台提供 ...
- Android 获取渠道名称
直接看代码, //获取渠道名称public static String getChannelName(Activity ctx) { if (ctx == null) { return null; } ...
- 【转】 C# 小技巧之获取变量名称
link: http://www.cnblogs.com/gongy/p/lm-2015-04-03.html 今天在自我规范程序设计的时候,变量名匹配字符串来自配置文件,网上找了一会儿发现也有朋友在 ...
- 根据UserAgent 获取操作系统名称
/// <summary> /// 根据 User Agent 获取操作系统名称 /// </summary> private sta ...
- linux c 通过文件描写叙述符获取文件名称
在linux中每一个被打开的文件都会在/proc/self/fd/文件夹中有记录,当中(/proc/self/fd/文件描写叙述符号:这个文件是符号文件)的文件就是文件描写叙述符所相应的文件. 而re ...
- JavaScript函数之实际参数对象(arguments) / callee属性 / caller属性 / 递归调用 / 获取函数名称的方法
函数的作用域:调用对象 JavaScript中函数的主体是在局部作用域中执行的,该作用域不同于全局作用域.这个新的作用域是通过将调用对象添加到作用域链的头部而创建的(没怎么理解这句话,有理解的亲可以留 ...
- Android 获取版本号名称工具类
package com.example.grenaderose.redthunder.utils; import android.content.Context; import android.con ...
- MVC 获取控制器名称和Action名称(转载)
MVC在filter中如何获取控制器名称和Action名称 使用ActionExecutingContext对象可以获取控制器名称.Action名称.参数名称以及参数值.路由和Action返回值不 ...
- MVC在filter中如何获取控制器名称和Action名称
使用ActionExecutingContext对象可以获取控制器名称.Action名称.参数名称以及参数值.路由和Action返回值不影响结果. 在代码中 [AttributeUsage(Attri ...
随机推荐
- C#编程(三十五)----------foreach和yield
枚举 在foreach语句中使用枚举,可以迭代集合中的元素,且无需知道集合中的元素个数. 数组或集合实现带GetEumerator()方法的IEumerable接口.GetEumerator()方法返 ...
- Ubuntu Linux下安装Oracle JDK
from://http://blog.csdn.net/gobitan/article/details/24322561 Ubuntu Linux下安装Oracle JDK Dennis Hu 201 ...
- 从零开始写一个发送h264的rtsp服务器(上)
转自:http://blog.csdn.net/jychen105/article/details/47006345 一.什么是RTSP 通常所说的rtsp协议其实包含三个协议: rtsp协议, rt ...
- mysql between and 遇到日期查询边界问题
最近实现一个按日期范围查询列表,例如输入的是日期 2015-11-01到2015-11-03,想得到1号到3号的数据, 执行 select * from table where create_date ...
- 根据ImageView的大小来压缩Bitmap,避免OOM
Bitmap是引起OOM的罪魁祸首之一,当我们从网络上下载图片的时候无法知道网络图片的准确大小,所以为了节约内存,一般会在服务器上缓存一个缩略图,提升下载速度.除此之外,我们还可以在本地显示图片前将图 ...
- Emulator: glTexImage2D: got err pre :( 0x502 internal 0x1908 format 0x1908 type 0x1401
Go to Tools > AVD Manager > Virtual device configuration > Show advanced settings > Boot ...
- vmware桥接模式
vmware桥接模式 桥接网络是指本地物理网卡和虚拟网卡通过VMnet0虚拟交换机进行桥接,物理网卡和虚拟网卡在拓扑图上处于同等地位,那么物理网卡和虚拟网卡就相当于处于同一个网段,虚拟交换机就相当于一 ...
- verilog语法学习目录
verilog语法实例学习(1) Verilog中的注释 Verilog中的信号 标识符 信号的值 Verilog中的数字 Verilog中的参数 verilog语法实例学习(2) 线网类型 变量类型 ...
- 奇怪吸引子---Aizawa
奇怪吸引子是混沌学的重要组成理论,用于演化过程的终极状态,具有如下特征:终极性.稳定性.吸引性.吸引子是一个数学概念,描写运动的收敛类型.它是指这样的一个集合,当时间趋于无穷大时,在任何一个有界集上出 ...
- 阿里NLP总监分享-NLP技术的应用与思考
https://yq.aliyun.com/articles/78031 NLP技术的应用及思考