如何在/ WEB-INF / [英] how to create files under /WEB-INF/
问题描述
我正在处理将文件存储在/ WEB-INF / someFolder /下的应用程序。但我没有找到正确的方法来创建这个文件夹下的文件。我这样做,但它不工作:
$ p $ 文件newFile =新文件(/ WEB-INF / fileName.xml );
当我尝试检查创建时:
boolean isCreated = newFile.createNewFile();
我得到:
java.io.IOException:没有这样的文件或目录
请帮助我它正确的方式。
更新:
我做了这个解决方法,它工作,但我没有看到它是高性能解决方案。
ServletContext servletContext = getServletContext();
String path = servletContext.getRealPath(/ WEB-INF /);
文件newFile2 = new File(path +/ fileName.xml);
有什么想法?
你可以得到这样的路径:
$ b $ pre $
String path = Thread.currentThread()。getContextClassLoader()。的getResource( COM / youpackage /);
现在你得到了你的类文件夹路径的路径,所以你可以得到WEB-INF路径。
ps:我记得创建文件时,你必须写一些内容,否则它可能不会创建。
I am working on an application that stores files under /WEB-INF/someFolder/. But I dont find the right way to create files under this folder. I did this, but it is not working:
File newFile = new File("/WEB-INF/fileName.xml");
When I try to check the creation:
boolean isCreated = newFile.createNewFile();
I get :
java.io.IOException: No such file or directory
Please help me doing it in the right way.
Update: I did this workaround, it is working but I dont see that it is performant solution.
ServletContext servletContext = getServletContext();
String path = servletContext.getRealPath("/WEB-INF/");
File newFile2 = new File(path+"/fileName.xml");
Any ideas?
make sure you applaction have the permissions to write. you can get the path ues like this:
String path=Thread.currentThread().getContextClassLoader().getResource("com/youpackage/");
Now you get the path which is your class folder path,so you can get the WEB-INF path. ps: i remember when create file you must writer some content,otherwies it may not create.
这篇关于如何在/ WEB-INF /的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!