java字符串文件流转解码

1、文件流数据转解码

1-1、文件流转码
 Base64.getEncoder().encodeToString(outputStream.toByteArray())
1-2、文件流解码
 Base64.getDecoder().decode(outputStream.toByteArray());

文件流数据转解码事例:

    public static void main(String[] args) throws FileNotFoundException {
        File file = new File("d:/6d486119-9642-4cd8-8d9c-3522be48a761.com-木材-木纹-8733.jpg");
        try (InputStream fileOutputStream = new FileInputStream(file)) {
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int length;
            while ((length = fileOutputStream.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            String encodeToString = Base64.getEncoder().encodeToString(output.toByteArray());
            System.out.printf("encodeToString=" + encodeToString);
            byte[] bb = Base64.getDecoder().decode(encodeToString);
            new FileOutputStream(new File("d:/aa.jpg")).write(bb);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }

2、字符串转解码

2-1、字符串转码
 URLEncoder.encode("转码中文","utf-8")
2-2、字符串解码
 URLDecoder.decode("%E8%BD%AC%E7%A0%81%E4%B8%AD%E6%96%87","utf-8")

你可能感兴趣的:(java,jvm,开发语言)