在Web Project中创建一个文件夹?

时间:2014-06-25 03:04:58

标签: jsp tomcat directory

我的网络项目是Tomcat + jsp。当我尝试在此项目中创建一个文件夹时,我的代码中没有异常,并且始终显示show folder。但是,我找不到目录中的文件夹。

我的代码如下:

private static final String imagFolder="pictures";
private static final String Separator=java.io.File.separator;
String directory = request.getContextPath()+Separator+imagFolder;
CreateFileUtil.createDir(directory);

***** CreateFileUtil.createDir ************************************ *

public static  boolean createDir( String destDirName) {
    File dir = new File(destDirName);
    if (dir.exists()) {// in this line,there is not exception and return true however, when I check the direcotry ,there folder doesn't exists
        System.out.println("create directory" + destDirName + "Failure, it already exists");
        return false;
    }
    if (!destDirName.endsWith(File.separator)) {
        destDirName = destDirName + File.separator;
    }
    //create
    if (dir.mkdirs()) {
        System.out.println("create directory" + destDirName + "Success");
        return true;
    } else {
        System.out.println("create directory" + destDirName + "Failure");
        return false;
    }
}

1 个答案:

答案 0 :(得分:0)

最后问题解决了。

我只是更改了代码:

String directory = request.getContextPath()+Separator+imagFolder;

 String directory=  request.getServletContext().getRealPath("/")+Separator+imagFolder;

然后它成功了。