relative path - How to save generated file temporarily in servlet based web application -
i trying generate xml file , save in /web-inf/pages/
.
below code uses relative path:
file folder = new file("src/main/webapp/web-inf/pages/"); streamresult result = new streamresult(new file(folder, filename));
it's working fine when running application on local machine (c:\users\username\desktop\source\myproject\src\main\webapp\web-inf\pages\myfile.xml).
but when deploying , running on server machine, throws below exception:
javax.xml.transform.transformerexception: java.io.filenotfoundexception c:\project\eclipse-jee-luna-r-win32-x86_64\eclipse\src\main\webapp\web inf\pages\myfile.xml
i tried getservletcontext().getrealpath()
well, it's returning null
on server. can help?
never use relative local disk file system paths in java ee web application such new file("filename.xml")
. in depth explanation, see getresourceasstream() vs fileinputstream.
never use getrealpath()
purpose obtain location write files. in depth explanation, see what servletcontext.getrealpath("/") mean , when should use it.
never write files deploy folder anyway. in depth explanation, see recommended way save uploaded files in servlet application.
always write them external folder on predefined absolute path.
either hardcoded:
file folder = new file("/absolute/path/to/web/files"); file result = new file(folder, "filename.xml"); // ...
or configured in one of many ways:
file folder = new file(system.getproperty("xml.location")); file result = new file(folder, "filename.xml"); // ...
or making use of container-managed temp folder:
file folder = (file) getservletcontext().getattribute(servletcontext.tempdir); file result = new file(folder, "filename.xml"); // ...
or making use of os-managed temp folder:
file result = file.createtempfile("filename-", ".xml"); // ...
the alternative use (embedded) database.
see also:
- recommended way save uploaded files in servlet application
- where place , how read configuration resource files in servlet based application?
- simple ways keep data on redeployment of java ee 7 web application
- store pdf limited time on app server , make available download
- what servletcontext.getrealpath("/") mean , when should use it
- getresourceasstream() vs fileinputstream
Comments
Post a Comment