职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 380|回复: 1

根据图片网络地址获取图片的byte[]类型数据

[复制链接]
爱车车 发表于 2011-8-30 10:19 | 显示全部楼层 |阅读模式
        /**
* 根据图片网络地址获取图片的byte[]类型数据
*
* @param urlPath
*            图片网络地址
* @return 图片数据
*/

  
public static byte[] getImageFromURL(String urlPath) {   
        byte[] data = null;   
        InputStream is = null;   
        HttpURLConnection conn = null;   
        try {   
            URL url = new URL(urlPath);   
            conn = (HttpURLConnection) url.openConnection();   
            conn.setDoInput(true);   
            // conn.setDoOutput(true);   
            conn.setRequestMethod("GET");   
            conn.setConnectTimeout(6000);   
            is = conn.getInputStream();   
            if (conn.getResponseCode() == 200) {   
                data = readInputStream(is);   
            } else{   
                data=null;   
            }   
        } catch (MalformedURLException e) {   
            e.printStackTrace();   
        } catch (IOException e) {   
            e.printStackTrace();   
        } finally {   
            try {   
                if(is != null){   
                    is.close();   
                }                  
            } catch (IOException e) {   
                e.printStackTrace();   
            }   
            conn.disconnect();            
        }   
        return data;   
    }  

public static byte[] getImageFromURL(String urlPath) {
                byte[] data = null;
                InputStream is = null;
                HttpURLConnection conn = null;
                try {
                        URL url = new URL(urlPath);
                        conn = (HttpURLConnection) url.openConnection();
                        conn.setDoInput(true);
                        // conn.setDoOutput(true);
                        conn.setRequestMethod("GET");
                        conn.setConnectTimeout(6000);
                        is = conn.getInputStream();
                        if (conn.getResponseCode() == 200) {
                                data = readInputStream(is);
                        } else{
                                data=null;
                        }
                } catch (MalformedURLException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                } finally {
                        try {
                                if(is != null){
                                        is.close();
                                }                               
                        } catch (IOException e) {
                                e.printStackTrace();
                        }
                        conn.disconnect();                       
                }
                return data;
        }


     /**
* 读取InputStream数据,转为byte[]数据类型
* @param is
*            InputStream数据
* @return 返回byte[]数据
*/

  
public static byte[] readInputStream(InputStream is) {   
        ByteArrayOutputStream baos = new ByteArrayOutputStream();   
        byte[] buffer = new byte[1024];   
        int length = -1;   
        try {   
            while ((length = is.read(buffer)) != -1) {   
                baos.write(buffer, 0, length);   
            }   
            baos.flush();   
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
        byte[] data = baos.toByteArray();   
        try {   
            is.close();   
            baos.close();   
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
        return data;   
    }  

public static byte[] readInputStream(InputStream is) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int length = -1;
                try {
                        while ((length = is.read(buffer)) != -1) {
                                baos.write(buffer, 0, length);
                        }
                        baos.flush();
                } catch (IOException e) {
                        e.printStackTrace();
                }
                byte[] data = baos.toByteArray();
                try {
                        is.close();
                        baos.close();
                } catch (IOException e) {
                        e.printStackTrace();
                }
                return data;
        }  

Jethro 发表于 2011-8-30 10:20 | 显示全部楼层
推荐链接
3G培训就业月薪平均7K+,不3K就业不花一分钱!
20-30万急聘多名天才Java/MTA软件工程师

见证又一个准百万富翁的诞生!

您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-4-27 15:49 , Processed in 0.117392 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表