文件转为字节数组工具类

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;
    }

 

上一篇:【客户下单】后台提供webservice服务接收数据


下一篇:读取数据流保存到指定的路径下