从http请求中读取图片,写到本地的方法

BufferedInputStream bis = null;
        ByteArrayOutputStream bos = null;
        FileOutputStream fos = null;
        try
        {
            bis = new BufferedInputStream(requset.getInputStream());
            bos = new ByteArrayOutputStream();
            int len = 0;
            // 读取
            byte[] buffer = new byte[409600];
            while ((len = bis.read(buffer)) > -1)
            {
                bos.write(buffer, 0, len);
            }
            byte[] allData = bos.toByteArray();

            // 写入
            fos = new FileOutputStream(new File("G://msc.jpg"));
            fos.write(allData);
            fos.flush();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {

            fos.close();
            bos.close();
            bis.close();
        }

你可能感兴趣的:(http)