Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing location of temp files created using Apache POI

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.

like image 669
ronak.patel Avatar asked Mar 14 '11 11:03

ronak.patel


People also ask

How do I change my TMP location?

To permanently set the temporary directory for a Windows user account, go to Control Panel->System->Advanced, and enter the "Environment Variables" dialog window to find and change the TEMP and TMP variable settings in the "User variables".

How do I redirect my temp folder?

Select the TEMP variable and then click the “Edit” button. In the Edit User Variable window, type a new path for the TEMP folder into the “Variable value” box. Alternatively, you can click the “Browse Directory” button to browse to your desired location.

Where are temp files created?

Temp files often have the extension . TMP and are stored in the C:\Users\AppData\Local\Temp folder. If you're working on a document, your word-processing app may create a temporary file to track your progress. If the app crashes, you'll be able to recover your data from the temp file.


2 Answers

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 ...!!!

like image 169
ronak.patel Avatar answered Nov 15 '22 20:11

ronak.patel


Here is how you can change the location from where POI reads the temporary files programmatically if you are not able to change system property "java.io.tmpdir"

File dir = new File("somepath");
dir.mkdir();
TempFile.setTempFileCreationStrategy(new DefaultTempFileCreationStrategy(dir));

This is driven by the Apache POI TempFile and DefaultTempFileCreationStrategy helper classes.

like image 39
D.Shev Avatar answered Nov 15 '22 19:11

D.Shev