java Spring ClassPathResource - 无法打开,因为它不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37198095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:17:08  来源:igfitidea点击:

Spring ClassPathResource - cannot be opened because it does not exist

javaspringspring-bootspring-integrationspring-integration-sftp

提问by bscott

UPDATE:I'll still keep Artem Bilan's answer marked as correct, but I still felt I needed to point this out for any future readers. It seems I was misunderstanding the concept of the 'default value' for the @Valueannotation. The functionality I was hoping to achieve by using

更新:我仍然会将 Artem Bilan 的答案标记为正确,但我仍然觉得我需要向任何未来的读者指出这一点。看来我误解了@Value注释的“默认值”的概念。我希望通过使用实现的功能

@Value("${someProp:defaultFilePath}")
private Resource resourcefilePath;

was that, should the file path someProp defined in application.properties throw an exception (i.e. file not found), it would then attempt to use the defaultFilePath (i.e. the one above). What defining a default value actually does is that, if the property itself (someProp) does not exist (not defined or commented out) in the application.properties file, it then attempts to use the default value instead.

也就是说,如果 application.properties 中定义的文件路径 someProp 抛出异常(即找不到文件),它将尝试使用 defaultFilePath(即上面的那个)。定义默认值的实际作用是,如果 application.properties 文件中不存在(未定义或注释掉)属性本身 (someProp),则它会尝试使用默认值。



I'm using Spring Integration Sftp for SSH file transfer. Using Spring Boot so no xml files. Before configuring the DefaultSftpSessionFactoryobject, I define a resource which contains a .txt file that has the private key required for the sFtp authentication.

我正在使用 Spring Integration Sftp 进行 SSH 文件传输。使用 Spring Boot 所以没有 xml 文件。在配置DefaultSftpSessionFactory对象之前,我定义了一个包含 .txt 文件的资源,该文件具有 sFtp 身份验证所需的私钥。

Previously, I used FileSystemResourcelike this:

以前,我FileSystemResource是这样使用的:

Resource resource = new FileSystemResource("C:/absolute/path/to/my/private/key/privateKey.txt");

This worked just fine. However, this application will eventually be put in a cloud environment, which means absolute paths like that won't work anymore. I'm trying to instead use ClassPathResource but it's not working no matter what I try. So far I've tried the following:

这工作得很好。但是,这个应用程序最终会被放到云环境中,这意味着这样的绝对路径将不再起作用。我正在尝试改用 ClassPathResource,但无论我尝试什么,它都不起作用。到目前为止,我已经尝试了以下方法:

Resource resource = new ClassPathResource("privateKey.txt");
Resource resource = new ClassPathResource("privateKey.txt", SomeClassInClassPath.class);
Resource resource = new ClassPathResource("com/my/package/name/privateKey.txt");

My directory structure looks something like this:

我的目录结构如下所示:

ProjectFolder -> src -> main -> java -> com -> my -> package -> name -> various java classes
                                                                        privateKey.txt
                             -> resources -> etc...

There's more but this is a simplified version of it. Can anyone help figure out how I can get it to recognize the path to my .txt? I keep getting java.io.FileNotFoundException: class path resource [resource] cannot be opened because it does not existno matter what I try.

还有更多,但这是它的简化版本。谁能帮我弄清楚如何让它识别我的 .txt 的路径?java.io.FileNotFoundException: class path resource [resource] cannot be opened because it does not exist无论我尝试什么,我都会不断获得。

EDIT:WAR structure:

编辑:War结构:

ProjectFolder -> META-INF -> maven -> etc..
              -> org -> springframework -> boot -> etc..
              -> WEB-INF -> classes -> com
                                    -> public
                                    -> application.properties
                                    -> privateKey.txt

回答by Artem Bilan

You show src -> main ->etc., but that doesn't matter for runtime.

您显示src -> main ->等,但这对运行时无关紧要。

If you really are going to have that file in the final application (jar? war?), be sure that you pack that file really as a part of the final application.

如果您真的要在最终应用程序(jar?war?)中包含该文件,请确保将该文件打包为最终应用程序的一部分。

And share that structure here.

并在此处分享该结构。

With Spring Java & Annotation Configuration you don't need to worry about ClassPathResourceobject. There is just enough to declare it like this in the appropriate @Configuration:

使用 Spring Java 和注解配置,您无需担心ClassPathResource对象。只需在适当的情况下像这样声明它就足够了@Configuration

@Value("com/my/package/name/privateKey.txt")
private Resource privateKey;