2

我在java中有一个文件上传servlet。我想设置上传文件夹的路径,让它在任何服务器上运行。我说:

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

但什么都没有保存。当我使用绝对路径上传文件夹时,一切正常。
那么如何设置服务器上传文件夹的路径呢?

4

2 回答 2

2

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.

于 2012-12-19T12:32:01.223 回答
2

我使用了下面的代码片段。它在 Windows 服务器上运行良好。

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();
        }
于 2012-12-19T12:33:48.097 回答