Henrique Santiago
Henrique Santiago

Reputation: 205

File path (JAVA EE)

I'm working in a web project and I'm having troubles with files(java.io.File). The problem is only when I run as web application(tomcat 7), if I run as Java Application, the problem doesn't exist.

When I instantiate a file, new File("dir");, its path become C://Windows/System32/dir, this way, i can't do anything, maybe cause don't have Windows privilegies.

"I solved the problem" passing new File("C://Users/user/dir"), but I don't like this solution. I wanted to do this automatically, get the app path, for example. I'm coding in my machine, and after the deploy.... i don't know.

Any tips?

That's the part of the project that I'm having the problem, a jsf bean. My view calls the method addFile() to save the file that I receive from my view. It's working, but i have to pass the path like I said before, like is on the code below. The path goes to the Windows dir System32

Bean.java

@ManagedBean
@ViewScoped
public class Bean {

    //ATTRIBUTES AND METHODS

    public void addFile() {

        File temporaryFile = new File("temporary");
        //...
        //...
        //...

    }
}

Sorry for my english, i'm brazillian.

Upvotes: 1

Views: 1220

Answers (2)

Shaaban Ebrahim
Shaaban Ebrahim

Reputation: 10422

you can add folder resources in web folder then add files as you want i tested it from adding some styles (css) and javascript and call them in jsf page.as example if you create file named x in folder files under web/resources folder

new File("files/x");

wishing the answer helps you .you can tell me if any problem happens again

Upvotes: 0

Björn Zurmaar
Björn Zurmaar

Reputation: 829

There is a method in the File class that does exactly what you're looking for: https://docs.oracle.com/javase/8/docs/api/java/io/File.html#createTempFile-java.lang.String-java.lang.String-

Alternatively you can assemble the file's path yourself:

File myTempFile = new File(System.getProperty("java.io.tmpdir"),"temporary");

Upvotes: 1

Related Questions