栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何将字节数组转换为Jar

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何将字节数组转换为Jar

这有点骇人听闻,但是他做了他的工作,这段代码基本上创建了一个伪造的url方案(myjarprotocol),当打开该方案时,它会返回该

jarBytes
字段(您的实际jar数据所在的位置)。然后通过反射它调用
SystemClassLoader.addURL
以URL作为他必须加载的jar参数。总之,SystemClassLoader被欺骗从伪造的url方案加载jar,该url方案返回您想要的任何InputStream。

public class JarLoader {    private static final byte[] jarBytes = new byte[] { 0x00  };    public static void main(String[] args) throws Exception {        URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() { public URLStreamHandler createURLStreamHandler(String urlProtocol) {     System.out.println("Someone asked for protocol: " + urlProtocol);     if ("myjarprotocol".equalsIgnoreCase(urlProtocol)) {         return new URLStreamHandler() {  @Override  protected URLConnection openConnection(URL url) throws IOException {      return new URLConnection(url) {          public void connect() throws IOException {}          public InputStream getInputStream() throws IOException {   System.out.println("Someone is getting my jar!!");   return new ByteArrayInputStream(jarBytes);          }      };  }         };     }     return null; }        });        System.out.println("Loading jar with fake protocol!");        loadJarFromURL(new URL("myjarprotocol:fakeparameter"));    }    public static final void loadJarFromURL(URL jarURL) throws Exception {        URLClassLoader systemClassloader = (URLClassLoader) ClassLoader.getSystemClassLoader();        Method systemClassloaderMethod = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);        systemClassloaderMethod.setAccessible(true);        systemClassloaderMethod.invoke(systemClassloader, jarURL);        // This make classloader open the connection        systemClassloader.findResource("/resource-404");    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/438741.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号