天天看點

通過http擷取到的圖檔,轉換成二進制

    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;

    }