/** * 文件转换为字节数组 * @param file * @return * @author ChenHua * create on 2011-12-9 下午1:40:14 */ public byte[] getBytesFromFile(File file) { try { FileInputStream is = new FileInputStream(file); long length = file.length(); if (length > Integer.MAX_VALUE) { return null; } byte[] bytes = new byte[(int)length]; int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { offset += numRead; } if (offset < bytes.length) { return null; } is.close(); return bytes; } catch (IOException e) { e.printStackTrace(); return null; } }
转载,不知道出处。