create temp files in tomcat webapps folder

筅森魡賤 提交于 2020-01-25 03:42:44

问题


I have a Google Web Project which works perfect on Development mode. Somewhere inside this project, I create some .xml files which I delete after parsing.

When I deploy the .war file of my project in Tomcat7 (var/lib/tomcat7/webapps) (I use the tomcat manage to deploy it) the project fails to create any file. I've tried all possible paths inside the webapps folder. I even tried context.getRealPath("/")+"/ROOT/tmp/" but nothing happens


回答1:


You should be using the temp folder instead of attempting to write directly to your webapp's deployment directory:

ServletContext app = (servlet).getServletContext();
File tmpDir = (File)app.getAttribute("javax.servlet.context.tempdir");
File targetFile = new File(tmpDir, "mytempfile.xml");
...

Remember to do everything in a try/catch block and properly clean-up your resources in the 'finally' block or you'll be sorry ;)



来源:https://stackoverflow.com/questions/10802242/create-temp-files-in-tomcat-webapps-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!