0

where is temp file written?

asked 2014-08-22 16:57:21 +0800

robertkaren gravatar image robertkaren
77 7

this may be tomcat question more than a zk question, but I would appreciate anyone's help. I am getting an xlsx file from elsewhere on server and downloading it from apache/cgi to zk zss using the code below (see downloadReportFile(),getBinFile()). It works fine but I notice when I run the server on my pc out of eclipse the file ends up being written in c:/eclipse-top-dir . When I run it on the unix server I can't find where it's being written but I assume it's also a common area. Is there a recommended way to keep temp files like this so that the files don't get written to by two sessions simultaneously (other than by using unique filename).

    public class RunReport extends SelectorComposer<Component> {
@Listen ("onClick = #okBtn")
public void onClickOKBtn(Event eve) {
    try {
        String format = formatRadiogroup.getSelectedItem().getId();
        String type = typeRadiogroup.getSelectedItem().getId();
        if (format.compareToIgnoreCase("spreadsheet") == 0) {
            //String srcFilename = "/zkspreadsheet_test.xlsx";
            String srcFilename = genTmpFilename(format, type);
            if (downloadReportFile(srcFilename, format, type)) {
                addTab(format, lblForReportTab(type),type, srcFilename);
            }
        } else if (format.compareToIgnoreCase("pdf") == 0) {
            Clients.showNotification("open pdf report", true);
        }
    } catch (Exception exe) {
        ErrorMessage.showErrorMsg("Unable to run report. The error message was: " + exe.getMessage(), "error", Messagebox.OK, Messagebox.ERROR);
    }
}
private boolean downloadReportFile(String filename, String format, String type) {
    try {
        long seconds = System.currentTimeMillis() / 1000l;
        User user = (User) User.getUserDataToSession();
        String urlParameters = "userId=" + user.getUserId() + "&f=GET_R...;
        OutputStreamWriter writer = null;
        URL url = new URL(User.urlBase + "/cgi-bin/ezt/get_report.tcl");
        URLConnection conn  = url.openConnection();
        conn.setDoOutput(true);
        writer = new OutputStreamWriter(conn.getOutputStream());
        writer.write(urlParameters);
        writer.flush();
        //reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        InputStream reader = conn.getInputStream();
        String result = getBinFile(filename, reader, writer);
        if (result != null) {
            return true;
        } else {
            return false;
        }
    } catch (Exception exe) {
        String m = "Unable to download report file.";
        if (exe.getMessage() != null && !exe.getMessage().isEmpty()) {
            m += " The error message was: " +exe.getMessage();
        }
        ErrorMessage.showErrorMsg(m, "Error", Messagebox.OK, Messagebox.ERROR);
        return false;
    }
}
private String getBinFile (String file, InputStream input, OutputStreamWriter writer) {
    try {
        byte[] buffer = new byte[4096];
        int n = - 1;

        OutputStream output = new FileOutputStream( file );
        while ( (n = input.read(buffer)) != -1)
        {
            if (n > 0)
            {
                output.write(buffer, 0, n);
            }
        }
        output.close();
        input.close();
        if (fileIsErrorMsg(file)) {
            return null;
        } else {
            //Clients.showNotification("downloaded " + file, true);
            return file;
        }
    } catch (IOException ioe) {
        String m = "IO Error downloading file.";
        if (ioe.getMessage() != null && !ioe.getMessage().isEmpty()) {
            m+= " The error message was: " + ioe.getMessage();
        }
        ErrorMessage.showErrorMsg(m, "Error", Messagebox.OK, Messagebox.ERROR);
        return null;
    } catch (Exception exe) {
        String m = "Error downloading file.";
        if (exe.getMessage() != null && !exe.getMessage().isEmpty()) {
            m+= " The error message was: " + exe.getMessage();
        }
        ErrorMessage.showErrorMsg(m, "Error", Messagebox.OK, Messagebox.ERROR);
        return null;
    }
}
private boolean fileIsErrorMsg(String file) {
    // if file is really an error message (it will begin with ERROR:, then display the message and return true.
    FileInputStream fis = null;
    try {
        String buf = "";
        File f = new File(file);
        int n;
        fis = new FileInputStream(f);
        if (fis.available() < 500) {
            while ((n = fis.read()) != -1) {
                buf += (char) n;
            }
        }
        if (!buf.isEmpty() && buf.startsWith("ERROR:")) {
            ErrorMessage.showErrorMsg(buf, "Error", Messagebox.OK, Messagebox.ERROR);
            return true;
        }
    } catch (IOException exe) {
        String m = "IO Error inspecting downloaded file.";
        if (exe.getMessage() != null && !exe.getMessage().isEmpty()) {
            m+= " The error message was: " + exe.getMessage();
        }
        ErrorMessage.showErrorMsg(m, "Error", Messagebox.OK, Messagebox.ERROR);
    } catch (Exception exe) {
        String m = "Error inspecting downloaded file.";
        if (exe.getMessage() != null && !exe.getMessage().isEmpty()) {
            m+= " The error message was: " + exe.getMessage();
        }
        ErrorMessage.showErrorMsg(m, "Error", Messagebox.OK, Messagebox.ERROR);
    } finally {
        try {
            if (fis != null) 
                fis.close();
        } catch (IOException ioe) {

        }
    }
    return false;
}
private String genTmpFilename(String format, String report) {
    User user = (User)User.getUserDataToSession();
    String userName = user.getAccount();
    long seconds = System.currentTimeMillis() / 1000l;
    String filename = userName + "_" + report + ".xlsx";
    // filename += seconds;
    return filename;
}
}
delete flag offensive retag edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2014-08-23 15:42:19 +0800

Darksu gravatar image Darksu
1991 1 4

Hello robertkaren,

The documentation of apache tomcat states that the temporary files are kept in the work dir. For more information please look at the link below:

https://tomcat.apache.org/tomcat-3.3-doc/serverxml.html

Regarding the file corruption, i would also set a unique name, or zip the files into an archive with a unique date-time format.

Best Regards,

Darksu

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2014-08-22 16:57:21 +0800

Seen: 8 times

Last updated: Aug 23 '14

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More