具有父目录引用的getResource [英] getResource with parent directory reference

查看:171
本文介绍了具有父目录引用的getResource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个java应用程序,我正在尝试加载将包含在jar中的文本文件。

I have a java app where I'm trying to load a text file that will be included in the jar.

当我做 getClass()。getResource(/ a / b / c /),它可以为该路径创建URL,我可以打印出来,一切都很好。

When I do getClass().getResource("/a/b/c/"), it's able to create the URL for that path and I can print it out and everything looks fine.

但是,如果我尝试 getClass()。getResource(/ a / b /../\"),那么我得到 null URL返回。

However, if I try getClass().getResource(/a/b/../"), then I get a null URL back.

似乎不喜欢 .. 在路上。任何人都看到我做错了什么?我可以发布更多代码,如果它会有所帮助。

It seems to not like the .. in the path. Anyone see what I'm doing wrong? I can post more code if it would be helpful.

推荐答案

normalize() FilenameUtils 类中的方法(其中有四个)它可以帮到你。它位于 Apache Commons IO库中。

final String name =  "/a/b/../";
final String normalizedName = FilenameUtils.normalize(name, true); // "/a/"
getClass().getResource(normalizedName);

这篇关于具有父目录引用的getResource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆