获取文件和图片的方法

来源:互联网 发布:淘宝卖家工具源码 编辑:程序博客网 时间:2024/05/16 14:45
public static String Settext(String urlstr) {    try {        URL url = new URL(urlstr);        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();        int responseCode = urlConnection.getResponseCode();        if (responseCode == 200) {            InputStream inputStream = urlConnection.getInputStream();            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));            String lin = "";            StringBuilder sb = new StringBuilder();            while ((lin = br.readLine()) != null) {                sb.append(lin);            }            return sb.toString();        } else {            //Toast.makeText(this,"当前网络不可用",Toast.LENGTH_LONG).show();        }    } catch (MalformedURLException e) {        e.printStackTrace();    } catch (IOException e) {        e.printStackTrace();    }    return "";}public static Bitmap Setimg(String urlstr) {    URL url = null;    try {        url = new URL(urlstr);        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();        int responseCode = urlConnection.getResponseCode();        if (responseCode == 200) {            InputStream inputStream = urlConnection.getInputStream();            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);            return bitmap;        } else {        }    } catch (MalformedURLException e) {        e.printStackTrace();    } catch (IOException e) {        e.printStackTrace();    }    return null;}