zoukankan      html  css  js  c++  java
  • 通过http URL 获取图片流 转为字节数组

     通过http URL 获取图片流 转为字节数组

     读取本地文件转为数组

    /**
         * 获取 文件 流
         * @param url
         * @return
         * @throws IOException
         */
        private byte[] getFile(String url) throws IOException{
            URL urlConet = new URL(url);
            HttpURLConnection con = (HttpURLConnection)urlConet.openConnection();    
            con.setRequestMethod("GET");    
            con.setConnectTimeout(4 * 1000);    
            InputStream inStream = con .getInputStream();    //通过输入流获取图片数据    
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();    
            byte[] buffer = new byte[2048];    
            int len = 0;    
            while( (len=inStream.read(buffer)) != -1 ){    
                outStream.write(buffer, 0, len);    
            }    
            inStream.close();    
            byte[] data =  outStream.toByteArray(); 
            return data;
        }
    /**
         * 读取 本地文件,转为字节数组
         * @param url 本地文件路径
         * @return
         * @throws IOException
         */
        private byte[] getImage(String url) throws IOException{
            BufferedInputStream in = new BufferedInputStream(new FileInputStream(url));
            ByteArrayOutputStream out = new ByteArrayOutputStream(1024);    
           
            byte[] temp = new byte[2048];        
            int size = 0;        
            while ((size = in.read(temp)) != -1) {        
                out.write(temp, 0, size);        
            }        
            in.close();  
            byte[] content = out.toByteArray();  
            return content;
        }
  • 相关阅读:
    [POJ 1269]Intersecting Lines
    [POJ 3304]Segments
    [HNOI 2011]数学作业
    [UOJ 12]猜数
    [UOJ 282]长度测量鸡
    [HAOI 2007]理想的正方形
    [POJ 2318]TOYS
    [SDOI 2009]HH的项链
    [USACO 12DEC]Running Away From the Barn
    [HDU 2036]改革春风吹满地
  • 原文地址:https://www.cnblogs.com/lemon-flm/p/9115598.html
Copyright © 2011-2022 走看看