Java 在 Spring 中将文件写入资源文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27909583/
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-08-11 05:17:09  来源:igfitidea点击:

Write a file to resources folder in Spring

javaspring

提问by Giancarlo Ventura Granados

I want to know, how to write a file to a resources folder in a Spring MVC project. I defined the resources path in a web-dispatcher-servlet.xmlas
<mvc:resources mapping="/resources/**" location="/resources/" />

我想知道,如何将文件写入 Spring MVC 项目中的资源文件夹。我将资源路径定义web-dispatcher-servlet.xml
<mvc:resources mapping="/resources/**" location="/resources/" />

I read examples about how to read a file from a resources folder. But I want to write a file to a resources folder. I tried

我阅读了有关如何从资源文件夹中读取文件的示例。但我想将文件写入资源文件夹。我试过

ClassLoader classLoader = getClass().getClassLoader();
    File file = new File(classLoader.getResource("file/test.xml").getFile());
    if (file.createNewFile()) {
        System.out.println("File is created!");
    } else {
        System.out.println("File already exists.");
    }

But I get a
Request processing failed; nested exception is java.lang.NullPointerException

但我得到一个
Request processing failed; nested exception is java.lang.NullPointerException

采纳答案by Reid Harrison

It looks like your call to getResource("file/test.xml")is likely returning null.

看起来您对getResource("file/test.xml") 的调用可能返回 null。

I'm curious, what is the full path to your XML file? For this to work, the resourcesdirectory needs to be placed in your webappdirectory. If you are trying to use the standard Java resources structure (src/main/resources) then that Spring MVC mapping will not work.

我很好奇,你的 XML 文件的完整路径是什么?为此,需要将资源目录放置在您的webapp目录中。如果您尝试使用标准 Java 资源结构 (src/main/resources),那么 Spring MVC 映射将不起作用。

EDIT: After seeing your answer to @Ascalonian's comment, this will not work since the file doesn't exist. Like I mentioned earlier, getResource("file/test.xml")will return null so the following call to getFile()will throw the NPE. Maybe you should check if getResourcereturns null and use that as an indication that the file needs to be created.

编辑:在看到你对@Ascalonian 的评论的回答后,这将不起作用,因为该文件不存在。就像我之前提到的那样,getResource("file/test.xml")将返回 null,因此对getFile()的以下调用将抛出 NPE。也许您应该检查getResource 是否返回 null 并将其用作需要创建文件的指示。

回答by Shawn Bush

If you want to create a test.xmlfile in the directory returned by getResource(), try this:

如果要test.xml在 返回的目录中创建文件getResource(),请尝试以下操作:

File file = new File(classLoader.getResource(".").getFile() + "/test.xml");
if (file.createNewFile()) {
    System.out.println("File is created!");
} else {
    System.out.println("File already exists.");
}

Calling getFile()on a non-existent directory or file will return null, as explained in Reid's answer.

调用getFile()不存在的目录或文件将返回 null,如 Reid 的回答中所述。

回答by Sahil Chhabra

Firstly, you should not write to a file in resource folder, as it will get deleted whenever you do a fresh build. Instead store it in some other location and specify the path in a property file.

首先,您不应该写入资源文件夹中的文件,因为每当您进行全新构建时它都会被删除。而是将其存储在其他位置并在属性文件中指定路径。

You can use the following way to create a file :

您可以使用以下方式创建文件:

String rootPath = System.getProperty("user.dir");
File file = new File(StringUtils.join(rootPath, "/any/path/from/your/project/root/directory/" , "test.xml"));
//Below commented line is what you wish to do. But I recommend not to do so.
//File file = new File(StringUtils.join(rootPath, "/out/resources/file/" , "test.xml"));
file.createNewFile();