简体   繁体   English

从URL加载JAR文件

[英]Loading a JAR file from a URL

How can I load a JAR file from a URL in Java while making the request for the file seem legit by adding necessary HTTP Headers into the request. 如何通过在Java中从URL加载JAR文件,同时通过在请求中添加必要的HTTP标头来使对该文件的请求看起来合法。

The only way I know how to maintain the loaded JAR file at the moment is to use ClassLoader . 我知道目前如何维护已加载的JAR文件的唯一方法是使用ClassLoader

I think I nailed this question pretty good (Or so it seems that way in my head!) but if anyone needs me to provide more information before they can help me, be sure to ask me. 我认为我对这个问题的回答很不错(或者在我脑海中似乎是这样!),但是如果有人需要我提供更多信息才能帮助我,请一定要问我。

You may be able to pass specific HTTP headers by passing a custom URLStreamHandlerFactory in a URLClassLoader constructor . 您可以通过在URLClassLoader构造函数中传递自定义URLStreamHandlerFactory来传递特定的HTTP标头。

Note that this is slightly different from creating the URLClassLoader using newInstance as it doesn't use checkPackageAccess from the SecurityManager (so you might have to do that yourself). 请注意,这与使用newInstance创建URLClassLoader稍有不同,因为它不使用来自SecurityManager checkPackageAccess (因此您可能必须自己做)。

Alternatively, perhaps jcloader could be of interest (but I haven't tried it): it might be easier to make a more traditional HTTP request (without using a URLStreamHandlerFactory ), potentially with the HTTP headers you want, and then load the result using one of its classloaders (as an InputStream or something similar). 另外,也许jcloader可能是您感兴趣的(但是我还没有尝试过):可能会更简单地发出一个更传统的HTTP请求(不使用URLStreamHandlerFactory ),可能带有所需的HTTP标头,然后使用其类加载器之一(作为InputStream或类似的东西)。

(There are of course other security implications for loading a jar file from a remote source, as you may need to verify you trust the integrity of this file.) (从远程源加载jar文件当然还有其他安全隐患,因为您可能需要验证自己是否信任此文件的完整性。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM