现在的位置: 首页 > 综合 > 正文

通过http获取到的图片,转换成二进制

2013年01月23日 ⁄ 综合 ⁄ 共 693字 ⁄ 字号 评论关闭

    public static byte[] getByte(String imgUrl) {

        try {
            URL url = new URL(imgUrl);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();

            // connection.getResponseCode();
            connection.setConnectTimeout(10000);
            BufferedImage image = ImageIO.read(connection.getInputStream());

            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ImageIO.write(image, "jpg", out);

            return out.toByteArray();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;

    }

抱歉!评论已关闭.