美文网首页
Base64 使用

Base64 使用

作者: 流水潺湲 | 来源:发表于2018-05-16 11:57 被阅读16次

最近在开发中,遇到了要在移动端上传图片需要base64

  /**
     * base64 编码
     *
     * @param path
     * @return
     * @throws Exception
     */
    public static String encodeBase64File(String path) {
        try {
            File file = new File(path);
            FileInputStream inputFile = new FileInputStream(file);
            byte[] buffer = new byte[(int) file.length()];
            inputFile.read(buffer);
            inputFile.close();
            return Base64.encodeToString(buffer, Base64.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * Base64解码
     * decoderBase64File:(将base64字符解码保存文件)\
     *
     * @param base64Code 编码后的字串
     * @param savePath   文件保存路径
     */
    public static void decoderBase64File(String base64Code, String savePath) throws Exception {
        byte[] buffer = Base64.decode(base64Code, Base64.DEFAULT);
        FileOutputStream out = new FileOutputStream(savePath);
        out.write(buffer);
        out.close();
    }

相关文章

网友评论

      本文标题:Base64 使用

      本文链接:https://www.haomeiwen.com/subject/dmlpdftx.html