1.定义要调用的java类和方法
package com.leywei.leweiCustomer.common.utils;
import java.io.UnsupportedEncodingException;
import sun.misc.*;
/**
* base64工具类
* Copyright: Copyright (c) 2019 binbin-United tech
*
* @ClassName: Base64Util.java
* @Description: 该类的功能描述
*
* @version: v1.0.0
* @author: hukuantao
* @date: 2019年6月6日 下午3:09:46
* @company 缤缤联合科技
*/
public class Base64Util {
/**
* base64加密字符串
* @author: hukuantao
* @date: 2019年6月6日 下午3:09:59
* @param str
* @return
*/
public static String getBase64(String str) {
byte[] b = null;
String s = null;
try {
b = str.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (b != null) {
s = new BASE64Encoder().encode(b);
}
return s;
}
/**
* base64解密字符串
* @author: hukuantao
* @date: 2019年6月6日 下午3:10:12
* @param s
* @return
*/
public static String getFromBase64(String s) {
byte[] b = null;
String result = null;
if (s != null) {
BASE64Decoder decoder = new BASE64Decoder();
try {
b = decoder.decodeBuffer(s);
result = new String(b, "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
public static void main(String[] args) {
/*String base64 = getBase64("12");
System.out.println(base64);*/
System.out.println(getFromBase64("MTIzNDU2"));
}
}
2.在项目的WEB-INF 下创建tld文件夹和tld文件(注意:可以放入自己新建的文件夹中,但不能放在lib和classess目录中)
3.在tld文件中创建内容
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd">
<tlib-version>1.0</tlib-version>
<short-name>Base64Util</short-name>
<uri>http://www.leywei.com/Base64Util</uri>//设置引用时的url
<function>
<name>getBase64</name>//方法的名称
<function-class>com.leywei.leweiCustomer.common.utils.Base64Util</function-class>//类的全路径名称
<function-signature>java.lang.String getBase64(java.lang.String)</function-signature>//返回参数类型 方法及参数类型
</function>
</taglib>
4.jsp页面中导入java类
<%@ taglib uri="http://www.leywei.com/Base64Util" prefix="base64"%>
5.el表达式引用
${base64:getBase64(share.id)}