将文件File转换成byte数组

代码如下:

/**
 * 将文件转换成byte数组
 * @param filePath
 * @return
 */
public static byte[] File2byte(File tradeFile){
    byte[] buffer = null;
    try
    {
        FileInputStream fis = new FileInputStream(tradeFile);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] b = new byte[1024];
        int n;
        while ((n = fis.read(b)) != -1)
        {
            bos.write(b, 0, n);
        }
        fis.close();
        bos.close();
        buffer = bos.toByteArray();
    }catch (FileNotFoundException e){
        e.printStackTrace();
    }catch (IOException e){
        e.printStackTrace();
    }
    return buffer;
}
原文地址:https://www.cnblogs.com/henuyuxiang/p/11812157.html