java base64 + BASE64Encoder 解决base64加密后的特殊字符被拦截问题

本文介绍了如何在Java中使用Base64进行字符串编码和解码,通过实例展示了如何编码一个包含HTML标签的字符串,并能正确地进行解码还原。
部署运行你感兴趣的模型镜像

package com.test;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Base64AndURLDecoder {
    final static BASE64Encoder encoder = new BASE64Encoder();
    final static BASE64Decoder decoder = new BASE64Decoder();
    public static String encode(String str) throws UnsupportedEncodingException{
         str=URLEncoder.encode(str,"UTF-8");
         byte[] textByte = str.getBytes("UTF-8");
         // 编码
         String encodedText = encoder.encode(textByte);
         return encodedText;
    }
    public static String decode(String str) throws IOException{
            byte[] textByte = decoder.decodeBuffer(str);
            String dString=new String(textByte,"UTF-8");
        return URLDecoder.decode(dString,"UTF-8");
   }
    public static void main(String[] args) throws Exception {
        String string="<p>测试</p>" ;
        String string2=encode(string);
        System.out.println(string2);
        System.out.println(decode(string2));
    }
}
 

您可能感兴趣的与本文相关的镜像

Seed-Coder-8B-Base

Seed-Coder-8B-Base

文本生成
Seed-Coder

Seed-Coder是一个功能强大、透明、参数高效的 8B 级开源代码模型系列,包括基础变体、指导变体和推理变体,由字节团队开源

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值