博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
通过httpclient3实现文件下载以及获取文件下载名称
阅读量:4947 次
发布时间:2019-06-11

本文共 3022 字,大约阅读时间需要 10 分钟。

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

 

转载于:https://www.cnblogs.com/yshyee/p/8623295.html

你可能感兴趣的文章
常用函数
查看>>
for循环
查看>>
[bzoj4872] [洛谷P3750] [六省联考2017] 分手是祝愿
查看>>
Shiro Quartz之Junit測试Session管理
查看>>
lunix shell 基础经常使用整理
查看>>
hdu 4961 Boring Sum(数学题)
查看>>
为什么没有好用的Android游戏引擎?
查看>>
Achieving High Availability and Scalability - ARR and NLB
查看>>
jQuery的deferred对象详解
查看>>
命令行连WiFi
查看>>
PHP扩展下载指导
查看>>
练习7第四题
查看>>
CodeForces - 633D Fibonacci-ish 大数标记map+pair的使用
查看>>
MyBatis定义复合主键
查看>>
Memcache+Tomcat9集群实现session共享(非jar式配置, 手动编写Memcache客户端)
查看>>
C和OC的不同
查看>>
JAVA基础代码分享--DVD管理
查看>>
关于Nginx499、502和504的分析
查看>>
MVC复杂类型的模型绑定
查看>>
Active Record: 資料庫遷移(Migration) (转)
查看>>