views:

52

answers:

2

Basically my problem is a that I need to copy an uploaded file (which is placed in a temporary folder on my server providers Tomcat ie. not a local Tomcat).

The code I'm using works when I deploy my project on my local machine but stops working when I deploy it live. I've found out that it has something to with my permissions in java.policy.

What I need to find out is how do I get access to the folder in which Tomcat stores the temporary file using Java.

When reading catalina.out this is the clue that the log gives me.

/usr/local/tomcat/work/Catalina/project name here/context of project here/upload_490341a6_12b1d397355_76ce_00000001.tmp

I'm thinking somewhere along the lines (note: this is not an actual method :P )

ServletActionContext.getContext().getSuperHiddenTemporaryCatalog();

The code snippet at the bottom has one flaw.

sourceFile and targetFile points to the same directory at the moment.

I want the sourceFile path to be the temporary tomcat-folder.

Thanks in advance! :D


`public String saveImage(File file, String uploadedFileName) {

    String path = ServletActionContext.getServletContext().getRealPath("images");

    System.out.println(path);

    String fullFileName = path + "/" + uploadedFileName;

    System.out.println(fullFileName);

    boolean successful = false;

    try {

        File sourceFile = new File(fullFileName);

        File targetFile = new File(path + "/" + uploadedFileName);

        InputStream in = new FileInputStream(sourceFile);
        OutputStream out = new FileOutputStream(targetFile);

        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();


    } catch (Exception e) {

        successful = false;

        e.printStackTrace();

    }

    if (successful) {

        return "context of project/images/" + uploadedFileName;

    } else {

        return "";

    }

}`
A: 

The code I'm using works when I deploy my project on my local machine but stops working when I deploy it live. I've found out that it has something to with my permissions in java.policy.

Yes. This is an example of a Java security sandbox.

What I need to find out is how do I get access to the folder in which Tomcat stores the temporary file using Java.

You cannot circumvent the security sandbox (modulo some unpatched bug in your JVM). What you need to do is change the "java.policy" settings so that your webapp has permission copy the file to where it needs to be copied. You may need to discuss this with whoever has done the security design, etc for your production Tomcats.

Stephen C
So, if the provider does not want to change the security settings I would be better off providing my own tomcat on a VM (perhaps hosted by them) or skip this feature entirely... Thanks a lot for the help!Cleared it up and saved me a lot of time crying over this problem!
Henrik Andersson
A: 
File tempDir = (File) servletContext.getAttribute("javax.servlet.context.tempdir");

should give you access to your temporary directory in Tomcat. It would be strange if you could not at least read files from there.

gpeche
Thanks, this helped alot! Got the fileupload to work. This way also added more checks to see if the file is readable and so on! Thanks!
Henrik Andersson