zoukankan      html  css  js  c++  java
  • 图片转二进制

        public static byte[] toBinary(String filename) throws IOException{
            if(!ParameterChecker.startWithProtocol(filename)){
                if(filename.startsWith("/")){
                    filename = WebUtils.getAbsolutePath(filename);
                }
                return toBinary(new File( filename ));
            }
            
            URL url = new URL(filename);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = connection.getInputStream();
            return toBinary(inputStream);
        }
        
        public static byte[] toBinary(File file) throws IOException {
            BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
            return toBinary(bufferedInputStream);
        }
        
        public static byte[] toBinary(InputStream inputStream) throws IOException{
            int len = inputStream.available();
            byte[] bytes = new byte[len];
            int r = inputStream.read(bytes);
            if (len != r) {
                bytes = null;
                throw new IOException("读取文件不正确");
            }
            inputStream.close();
            return bytes;
        }
  • 相关阅读:
    <span>和<div>标签的隐藏和显示切换
    重启svn
    Mac下配置apache
    iOS时间显示今天昨天
    关于UIPageViewController那些事
    关于plist文件的那些事
    Xcode调试LLDB
    Reveal安装
    静态初始化器
    Static简介
  • 原文地址:https://www.cnblogs.com/rubekid/p/4535707.html
Copyright © 2011-2022 走看看