集册 Java实例教程 从HTTP下载返回InputStream

从HTTP下载返回InputStream

欢马劈雪     最近更新时间:2020-01-02 10:19:05

672
从HTTP下载返回InputStream


//package com.nowjava;// 来自 时 代 J a v a - nowjava.com

import java.io.IOException;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;


public class Main {

    /**

     * Download from HTTP, mustn't run in main thread, Caller need to apply permission themselves.

     * @param url

     * @return : null if there is some errors.

     */

    public static InputStream download(String url) {

        try {

            URL mUrl = new URL(url);

            HttpURLConnection conn = (HttpURLConnection) mUrl

                    .openConnection();

            conn.setConnectTimeout(10000);
            /** from 
            N o w J a v a . c o m - 时代Java**/

            conn.setReadTimeout(10000);

            InputStream is = conn.getInputStream();

            return is;

            //        conn.setInstanceFollowRedirects(false);

        } 
展开阅读全文