java - How to get the absolute path of a file in my project (and not of a file stored in a temporary folder) -
i using:
- eclipse java ee ide web developers version: mars.2 release (4.5.2);
- apache tomcat v8.0;
- a web dynamic project;
- a java servlet.
i have json file stored in ./webcontent
folder. trying absolute path of json file in way:
servletcontext sc = request.getsession().getservletcontext(); //string absolutepath = "/users/kazuhira/documents/mac_workspace/lab2_calendario/webcontent/database/events.json"; string relativepath = "eventsbackup.json"; string filepath = sc.getrealpath(relativepath); system.out.println("(saverservlet): path of file "+filepath); //system.out.println("(saverservlet): path of file "+absolutepath); //file file = new file(absolutepath); string content = request.getparameter("jsoneventsarray"); try (fileoutputstream fop = new fileoutputstream(file)) { system.out.println("(saverservlet): trying access file"+filepath); // if file doesn't exists, create if (!file.exists()) { system.out.println("(saverservlet): file doesn't exists"); file.createnewfile(); system.out.println("(saverservlet): file "+filepath+" created"); } system.out.println("(saverservlet): writing on file "+filepath); // content in bytes byte[] contentinbytes = content.getbytes(); fop.write(contentinbytes); fop.flush(); fop.close(); system.out.println("(saverservlet): events backup done"); } catch (ioexception e) { e.printstacktrace(); }
and file path reconstructed relativepath
is:
/users/kazuhira/documents/mac_workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/lab2_calendario/testjson.txt
why string filepath = sc.getrealpath(relativepath);
generates path on temporary folder? in way can configure context on "real" json file (the same create in project)?
i suppose tomcat working in temporary context, why? there's way tell him use same folder of project?
yes, can changing option in server configuration in eclipse
select second option in radio button list :)
Comments
Post a Comment