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;
   }      
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章