远程url文件地址转成byte

public static byte[] urlTobyte(String url) throws MalformedURLException {  
    URL ur = new URL(url);
    BufferedInputStream in = null;  
    ByteArrayOutputStream out = null;  
    try {  
        in = new BufferedInputStream(ur.openStream());  
        out = new ByteArrayOutputStream(1024);  
        byte[] temp = new byte[1024];  
        int size = 0;  
        while ((size = in.read(temp)) != -1) {  
            out.write(temp, 0, size);  
        }  
    } catch (Exception e) {  
        e.printStackTrace();  
    } finally {  
        try {  
            in.close();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
    byte[] content = out.toByteArray();  
    return content;  
} 

 

posted @ 2016-03-16 13:42  也许还年轻  阅读(5120)  评论(0编辑  收藏  举报