1.对字符BASE64加密
import sun.misc.BASE64Encoder;
import java.net.URLEncoder;
/**
* 对字符BASE64加密
* @param content
* @return
* @throws UnsupportedEncodingException
*/
@RequestMapping("encoder")
public String encoder(String content) throws UnsupportedEncodingException {
BASE64Encoder encoder = new BASE64Encoder();
String file = encoder.encode(content.getBytes());
String encode = URLEncoder.encode(file, "UTF-8");
log.info("encode:{}", encode);
return encode;
}
加密后的字符
2.对加密后的字符进行解码并保存成txt文件
import sun.misc.BASE64Decoder;
import java.net.URLDecoder;
/**
* 解密BASE64
* @param request
* @throws IOException
*/
@RequestMapping("decoder")
public void decoder(HttpServletRequest request) throws IOException {
// 获取BASE64加密后的字符
String encode = request.getParameter("encode");
String decode = URLDecoder.decode(encode, "UTF-8");
//解码base64
BASE64Decoder decoder = new BASE64Decoder();
byte[] bytes = decoder.decodeBuffer(decode);
//保存为文件,BASE64Decoder.txt最终会出现在项目的根目录中
OutputStream out = new FileOutputStream("BASE64Decoder.txt");
out.write(bytes);
out.flush();
out.close();
}
保存后的文件