文件转二进制流

/**
     * file->byte[]
     * 
     * @param file
     */
    public static byte[] fileToByte(File file) {
        byte[] byteArray = null;
        InputStream is;
        ByteArrayOutputStream baos;
        try {
            is = new FileInputStream(file);

            baos = new ByteArrayOutputStream(1024 * 1024);
            byte[] buf = new byte[1024 * 1024];
            int len;
            while ((len = is.read(buf)) != -1) {
                baos.write(buf, 0, len);
            }
            is.close();
            baos.flush();
            baos.close();
            byteArray = baos.toByteArray();
        } catch (FileNotFoundException e) {

        } catch (IOException e) {
            e.printStackTrace();
        }
        return byteArray;
    }

还需自行查阅源码,做健壮处理。

需要完善。

原文地址:https://www.cnblogs.com/zno2/p/4496838.html