博主首页

文件转为字节数组工具类/转base64

文件转base64

            try(
                    InputStream inputStream = new BufferedInputStream(fileOSSObject.getObjectContent())
            ){
                int available = inputStream.available();
                byte[] bytes = new byte[available];
                inputStream.read(bytes);
                java.util.Base64.Encoder encoder = java.util.Base64.getEncoder();
                String encode = encoder.encodeToString(bytes);
                getFileReData.setObjectBase64(encode);
            }catch (IOException e){
               throw new SupportPersonException(e.getMessage(),e);
            }

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

 

posted @ 2019-01-03 11:26  笑~笑  阅读(879)  评论(0编辑  收藏  举报