通过URL实现下载

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_38095094/article/details/75194608

通过URL和URLConnection实现下载

借鉴于《疯狂java讲义》
URL是指向互联网资源的指针。通常情况下,URL由协议名、主机、端口和资源组成,比如:Http://www.baidu.com/xxx.html
重要的方法:
* InputStream openStream() 打开和此URL的连接,返回用于读取该URL资源的InputStream,可以通过这个方法实现远程资源的下载,甚至可以实现多线程下载
* URLConnection和HttpURLConnection对象,前者表示应用程序和URL之间的连接,后者表示URL之间的HTTP连接。
* 创建一个URL连接,并发送请求、读取资源需要一下步骤:
- 通过URL的openConnection方法来获取URL的URLConnection对象
- 设置URLConnection的参数和普通请求对象
- 如果只是发送GET方式请求,则使用connect方法建立和远程资源之间的实际连接即可;如果需要发送POST请求,则需要获取URLConnection实例对应的输出流来发送请求参数
- 远程资源变为可用,程序之间可以访问远程资源的头字段或通过输入流读取远程资源的数据
* 一般来说,用setRequestProperty方法来设置或增加通用头字段,为该URLConnection的Key请求头字段增加value值,该方法不会覆盖原请求头字段的值,而是将新值追加到原请求头字段中。

    conn.setRequestProperty("Accept","*/*");    //*/*表示所有类型的文件

下面是一段向指定URL发送请求并获得资源下载的方法

  //用GET请求发送
        public static String sendGet(String url,String param){
            String result = "";
            String urlName = url+"?"+param;
            try{
                URL realURL = new URL(urlName);
                //打开和URL之间的连接  
                URLConnection conn = realURL.openConnection();
                //设置请求的通用属性  
                conn.setRequestProperty("accept","*/*");
                conn,setRequestProperty("Connection","Keep-Alive");
                conn.setRequestProperty("user-agent","Mozilla/4.0(compatible;MSIE  
                6.0;Windows NT 5.1;SV1)");
                //建立实际的连接  
                conn.connect();
                //获取所有响应的头字段  
                Map<String,List<String>> map = conn.getHeaderFields();
                //定义BufferedReader输入流来读取URL的响应  
                BudderedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(),
                "utf-8");
                String line ;
                while((line = in.readLine())!=null){
                    result+=line;
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    //用post方式向指定URL发送请求  
    public static String sendPost(String url,String param){
        String result = "";
        try{
            URL url = new URL(url);
            URLConnection conn = url.getConnection();
            //设置URLConnection请求
            conn.setRequestProperty("accept","*/*");
            conn.setRequestProperty("Connection","Keep-Alive");
            conn.setRequestProperty("user-agent","Mozilla/4.0(compatible;MSIE  
                6.0;Windows NT 5.1;SV1)");
            //发送Post请求必须设置doInput和doOutput
            conn.setDoOutput(true);
            conn.setDoInput(true);
            //获取URL对象输出流  
            PrintWriter out = new PrintWriter(conn.getOutputStream());
            //发送请求参数  
            out.print(param);
            out.flush();
            //post一定要记得发送请求参数  
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()),"utf-8");
            String line ; 
            while((line = br.readLine()!=null){
                result +=line ;
            }

        }catch(Exception e){
            e.printStackTrace();
        }
        return result;
    }

可以看出,用get和post请求的区别就是post需要先设置doInput和doOutput头字段的值,然后要得到URL对应的输出流,向URL发送请求参数。而get请求只需要直接使用connect方法。

猜你喜欢

转载自blog.csdn.net/qq_38095094/article/details/75194608