changing location of temp files created using Apache POI

ronak.patel picture ronak.patel · Mar 14, 2011 · Viewed 15.9k times · Source

I am stuck with an issue with reading .xlsx file. Some temporary files with random name are created under /tmp/poifiles directory whenever I use WorkbookFactory.create(inputStream);. This directory is created with RW-R-R- permission for the first user. So another user on the same machine when tries to access these files, he CANNOT.

Please suggest me any way

1) How can I create these temp files under /tmp directory and not always in /tmp/poifiles (I am using RHEL V5.0)

2) and how can I configure POI such as to change the location from where it reads the temporary files??

Anymore help to solve my problem of different users accessing same .xlsx files through POI is badly needed.

Answer

ronak.patel picture ronak.patel · Mar 16, 2011

Yuppie...I got the solution....

POI uses the following method to create temp files.

public static File createTempFile(String prefix, String suffix)
{
    if (dir == null) {
        dir = new File(System.getProperty("java.io.tmpdir"), "poifiles");
        dir.mkdir();
        if (System.getProperty("poi.keep.tmp.files") == null) {
            dir.deleteOnExit();
        }
    }
    File newFile = new File(dir, prefix + rnd.nextInt() + suffix);
    if (System.getProperty("poi.keep.tmp.files") == null) {
        newFile.deleteOnExit();
    }
    return newFile;
}

Now here as we can see it gets the location from property "java.io.tmpdir" and creates poifiles directory inside that...

I changed the location of java.io.tmpdir by setting this property (using System.setProperty("java.io.tmpdir", "somepath"))to user specific location..and Voila....Every user now can create temp files at location always accessible to them and not only the first user gets the privilege to create directory accessible only to him ...!!!