java GridFSDBFile图片文件 转base64

 @Autowired 
 DefaultGridFsTemplate dgt;
 
 public String test(){
        SimpleDateFormat sdfY = new SimpleDateFormat("yyyy");
        SimpleDateFormat sdfYM = new SimpleDateFormat("yyyyMM");
        Date now = new Date();
        GridFSDBFile file= dgt.findOne("5cf7260731d5530016f7343e", "CadrePerson_HeadImg");
        byte[] data = new byte[1024];
        InputStream in = file.getInputStream();
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();

        int len = -1;
        try {
            while((len = in.read(data)) != -1){
                swapStream.write(data,0,len);
            }
            in.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        
        BASE64Encoder encoder = new BASE64Encoder();
        String pictureBase = encoder.encode(swapStream.toByteArray()).trim();
        return pictureBase;
   }      

你可能感兴趣的:(java GridFSDBFile图片文件 转base64)