如何在java中的WEB-INF资源中以编程方式保存文件? [重复]

时间:2020-12-01 17:18:16

This question already has an answer here:

这个问题在这里已有答案:

The environment is MAVEN-based WILDFLY container.

环境是基于MAVEN的WILDFLY容器。

I can retrieve the URI of file as

我可以检索文件的URI为

Foo.class.getClassLoader().getResource("/images/nir.png");

how can I programmatically add a file on that folder?

如何以编程方式在该文件夹上添加文件?

I did something like this:

我做了这样的事情:

Foo.class.getClassLoader().getResource("/text/");
OutputStream stream = new FileOutputStream(newjava.io.File(loader.getFile()+"1.txt"));
//add something on 1.txt, save it on web-inf folder!!

and from web, if I had to connect to this. Why am I not able to do so?

如果我必须连接到此,请从网上获取。为什么我不能这样做?

like, to access to server

喜欢,访问服务器

http://server/text/1.txt

Why doesn't it work this way?

为什么不这样工作?

1 个解决方案

#1


Try this code. But it's not recommend

试试这个代码。但不建议这样做

String path = getServletContext().getRealPath("WEB-INF/../");
File file = new File(path);
String fullPathToYourWebappRoot = file.getCanonicalPath();

#1


Try this code. But it's not recommend

试试这个代码。但不建议这样做

String path = getServletContext().getRealPath("WEB-INF/../");
File file = new File(path);
String fullPathToYourWebappRoot = file.getCanonicalPath();