gpt4 book ai didi

java - 如何从远程服务器为位于 apache-tomcat WEB-INF/lib 文件夹下的 jar 文件创建一个下载 url?

转载 作者:行者123 更新时间:2023-11-28 23:24:20 27 4
gpt4 key购买 nike

谁能告诉我如何为位于 apache-tomcat/WEB-INF/lib 文件夹下的 jar 文件创建下载链接。我能够获得以下 jar 文件位置:

jar:file:/var/lib/tomcat7/webapps/cs/WEB-INF/lib/commons-fileupload-1.3.1.jar!/org/apache/commons/fileupload/disk/DiskFileItem.class

我想为这个 jar 文件构建一个下载 URL,以便我可以将它从远程服务器下载到我的本地硬盘。

我有以下用于查找 java 版本的代码:

public static Map<String, String> getJarVersions(ICS ics, ServletContext context)
{

ServletContextResource resource = new ServletContextResource(context, "/WEB-INF/lib/");

File file = null;
try
{
file = resource.getFile();
}
catch (IOException e)
{
log.error("Error reading /WEB-INF/lib/ directory. ", e);
return new HashMap(0);
}
File[] jarFiles = file.listFiles();
Map<String, String> jarNameVersion = new HashMap();
File[] arr$ = jarFiles;int len$ = arr$.length;
for (int i$ = 0; i$ < len$;)
{
File jarFile = arr$[i$];

JarFile jarfile = null;
Manifest manifest = null;
try
{
jarfile = new JarFile(jarFile);
manifest = jarfile.getManifest();
if (manifest != null)
{
Attributes attrs = manifest.getMainAttributes();
String version = attrs.getValue("Implementation-Version");
String vendor = attrs.getValue("Implementation-Vendor");
if (vendor == null) {
vendor = "Not applicable";
}
if (version == null) {
version = "Not applicable";
}
version = version + " : " + vendor;
jarNameVersion.put(jarFile.getName(), version);
}
else
{
jarNameVersion.put(jarFile.getName());
}
if (jarfile != null) {
try
{
jarfile.close();
}
catch (IOException e)
{
log.error("Error thrown while closing the jar file ");
}
}
i$++;
}
catch (IOException e)
{
log.error("Error reading JAR/MANIFEST file . ", e);
}
finally
{
if (jarfile != null) {
try
{
jarfile.close();
}
catch (IOException e)
{
log.error("Error thrown while closing the jar file ");
}
}
}
}
return jarNameVersion;
}
}

最佳答案

您不能直接访问 WEB-INF 文件夹下的任何资源,因为它受到 tomcat 的保护,免受 url 访问请求。

如果您需要从 WEB-INF 中访问任何资源,您应该构建一个 java 组件(servlet、过滤器、 Controller 等)来访问此 WEB-INF 元素并返回它们。

关于java - 如何从远程服务器为位于 apache-tomcat WEB-INF/lib 文件夹下的 jar 文件创建一个下载 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39162151/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com