Best practice saving temp files on tomcat?

I need a demo of an xml-based data exchange system. He defiantly shuts down on a trusted computer at school. The application will receive the database later, but only for this presentation I only need to show the layout, opening and saving of xml files and how the table receives data from xml.

So, what would be the best place for a web application to temporarily create xml files so that I can demonstrate this application? I use Eclipse and Tomcat.

As already mentioned, security is not a problem at all , since this version will NOT be online. In addition, it is ok if the files are deleted every time the application starts.

They should only exist for the duration of the presentation, where the application runs once. So, I do not know what will be the best place and how to get a path to such a location, independent of the computer used.

Thank.

+4
source share
4 answers

Use the property java.io.tmpdirto get a temporary folder tomcatand save your files there. This will be a good place to hold them temporarily. Thus, you can define it as follows:

public static final String TMP_DIR = System.getProperty("java.io.tmpdir")
+9
source

java.io.tmpdir javax.servlet.context.tempdir, . tomcat workDir .

servletContext.getAttribute("javax.servlet.context.tempdir")

. tomcat.

+6

, , File.createTempFile?

:

File temp = File.createTempFile("real",".howto");
temp.deleteOnExit();
+1

, temp

public static String getSystemFileLocation()
{
    File tmpFile = null;
    String fileLocation="/tmp/";
    if (System.getProperty("os.name").toUpperCase().indexOf("WINDOWS") == 0) {
        tmpFile=new File("c:\\temp");
        if(tmpFile.exists()) {
            fileLocation="c:\\temp\\";
        }
        else {
            tmpFile.mkdir();
            fileLocation="c:\\temp\\";
        }
    }
    else if (System.getProperty("os.name").toUpperCase().indexOf("WINDOWS") == -1) {
        fileLocation="/tmp/";   
    }
    return fileLocation;
}
-2

Source: https://habr.com/ru/post/1541380/


All Articles