Kliver Max
Kliver Max

Reputation: 5299

How to get Java web application path?

I have a fileupload servlet in java. And i want to set path to upload folder, to it waork on any server. I say:

 File disk = new File("/myportlet/upload/"+item.getName());
 item.write(disk);

But nothing saved. When i use absolute path to upload folder all work fine.
So how to set path to upload folder in server?

Upvotes: 2

Views: 1903

Answers (2)

Isaac
Isaac

Reputation: 16736

The leading "/" at the new File() constructor refers to the root of the file system. The file will be written into a directory named /myportlet/upload, in your code.

As the comments implied, writing into appserver-internal directories violates the spec and is generally a terrible idea - I honestly can't think of one proper use for doing so. What you want to do is to read the target path from a parameter - for example, a servlet's initialization parameter or a context initialization parameter - and use that.

Upvotes: 2

I used the below snippet. It worked fine in windows server.

File f=new File("sample.xls");
        f.createNewFile();
        FileOutputStream fos=null;
        if(f != null){
            fos=new FileOutputStream(f);
            fos.write(b);
            fos.flush();
            fos.close();
        }

Upvotes: 2

Related Questions