文件转为字节数组工具类
Posted qq376324789
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文件转为字节数组工具类相关的知识,希望对你有一定的参考价值。
java将文件转为字节数组
/** * 将文件转为字节 * @param listPd * @return */ public static byte[] t3(String path){ File file = new File("F:/util02/apache-tomcat-8.5.23/driverImgs/"+path); FileInputStream stream =null; ByteArrayOutputStream bos =null; byte[] bs; try { stream = new FileInputStream(file); bos = new ByteArrayOutputStream(); bos.write(stream); bs = bos.toByteArray(); } catch (Exception e) { // TODO Auto-generated catch block return new byte[1]; }finally { if(bos!=null){ try { bos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(stream!=null){ try { stream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return bs; }
以上是关于文件转为字节数组工具类的主要内容,如果未能解决你的问题,请参考以下文章