/** * 根据文件路径获取文件字节流 * @return * filePath 文件路径 * @throws IOException */ public static byte[] toByteArray(String filePath) throws IOException { File f = new File(filePath); if (!f.exists()) { throw new FileNotFoundException("文件不存在"); } ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length()); BufferedInputStream in = null; try { in = new BufferedInputStream(new FileInputStream(f)); int buf_size = 1024; byte[] buffer = new byte[buf_size]; int len = 0; while (-1 != (len = in.read(buffer, 0, buf_size))) { bos.write(buffer, 0, len); } return bos.toByteArray(); } catch (IOException e) { e.printStackTrace(); throw e; } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } bos.close(); } }
根据文件路径获取文件字节流
最新推荐文章于 2024-05-03 17:32:28 发布