入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

base64和byte[]相互转换

创建时间:2017-09-28 投稿人: 浏览次数:189
   /**
     * 上传图片获取图片base64码,然后解码,然后转成字节数组,以流的形式输出到本地
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/uploadYujhImg1", method = RequestMethod.POST)
    @ResponseBody
    public EntityServiceResponse<Map<String, String>> uploadYujhImg1(String fileImg) throws Exception {
        logger.info("uploadYujh"+fileImg);
        String imgBase64 = fileImg.replaceAll("data:image/png;base64,","");
        BASE64Decoder d = new BASE64Decoder();
        byte[] data = d.decodeBuffer(imgBase64);
        FileOutputStream os = new FileOutputStream("D:\tupian\d.jpg");
        os.write(data);
        os.close();
        return null;
    }
///////////////////////////////////////////////////////////////////////////////////////////
  
//读取本地图片输入流
//将图片文件转化为字节数组字符串,并对其进行Base64编码处理
   public static void  main (String arg[]) throws IOException {
        //读取本地图片输入流
        //将图片文件转化为字节数组字符串,并对其进行Base64编码处理
        String imgFile = "D:\tupian\a.jpg";//待处理的图片
        InputStream in = null;
        byte[] data = null;
        //读取图片字节数组
        try
        {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        //对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        System.out.println(encoder.encode(data));//返回Base64编码过的字节数组字符串
    }


 

声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
  • 上一篇:没有了
  • 下一篇:没有了
未上传头像