zoukankan      html  css  js  c++  java
  • 通过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

  • 相关阅读:
    codephp 自研PHP框架并实现composer包管理
    收藏!17 张程序员专属壁纸(使用频率很高)
    git reset hard HEAD^后显示more?的解决方案
    如果有一天我不得不离开IDE,没有其它原因,一定是ta ?
    centos7 下安装composer失败
    不知道如何技术变现?19个程序员接私活平台汇总
    MySQL常见面试题:什么是主从延时?如何降低主从延时?
    Nginx服务器,修改html 文件后页面不更新生效(已解决)
    《Microsoft SQL Server 2005: 数据库基础由入门到精通》书评
    讲座资源:Silverlight In Action
  • 原文地址:https://www.cnblogs.com/yshyee/p/8623295.html
Copyright © 2011-2022 走看看