WidthInEms
WidthInEms

Reputation: 11

Error in opening InputStream from temp directory

EDIT: I recommend against saving .jasper files to the temp directory and then attempting to load from that location. It's a lot of trouble.


I am attempting to add JasperReport functionality to a Spring web app. The idea is that a pre-compiled .jasper file can be added to the server's Temp folder and filled then exported by the user.

I can successfully read .jrxml from the classpath, compile it into a JasperReport, fill it, and export it within the same method. In trying to separate these tasks, I've run into trouble. Specifically, when attempting to open an input stream from the temp directory.

The following code successfully creates a compiled .jasper file in the temp directory (I have omitted checks against filename for brevity).

Resource res = appContext.getResource("classpath:jasperreports/"
            + filename + ".jrxml");

File f = new File(
            org.apache.commons.lang.SystemUtils.getJavaIoTmpDir(), filename
                    + ".jasper");       

JasperCompileManager.compileReportToStream(res.getInputStream(),
            new FileOutputStream(f));

Trying to READ from the temp folder causes a problem. After checking the temp folder for the newly-created .jasper, I call the following code.

Map<String, Object> params = new HashMap<String, Object>();

String resourcePath = org.apache.commons.lang.SystemUtils.getJavaIoTmpDir().getPath().toString();

Resource compiledReport = appContext.getResource(resourcePath + "/" + filename + ".jasper");
//Directly accessing the folder didn't work, either
//Resource compiledReport = appContext.getResource("C:/Users/<MyUsername Here>/AppData/Local/Temp/SimpleEmployeeReport.jasper");
JasperPrint filledReport = JasperFillManager.fillReport(compiledReport.getInputStream(), 
            params, mds.getConnection());

Which results in an exception thrown at getInputStream:

java.io.FileNotFoundException: Could not open ServletContext resource [/C:/Users/<MyUsername>/AppData/Local/Temp/SimpleEmployeeReport.jasper]

I'd appreciate any light you can shed on this!

Upvotes: 0

Views: 889

Answers (1)

devcd603
devcd603

Reputation: 231

Multipart held tmp files that caused new file names. Creating my own temp directory solved the issue. On Linux the getProperty is missing the trailing slash

    String tempDirectory = System.getProperty("java.io.tmpdir");
    if(  !tempDirectory .endsWith("/") && !tempDirectory .endsWith( "\\") ) {
        tempDirectory = tempDirectory +"/";

Upvotes: 1

Related Questions