我的web根目录下有一个文件夹,work,我要在work下动态生成一些新的文件夹,这是action部分的调用代码
CommonHelper.bulidDir(request.getContextPath()+"\\work\\"+albumName+"\\");//其中的albumName是从页面传入的值,这个是绝对存在的这是CommonHelper.bulidDir的代码
public static void bulidDir(String path)
{
try
{
File theFile = new File(path);
if (!theFile.exists())
{
theFile.mkdirs();
}
}
catch (Exception e)
{
System.out.println("can not make directory");
}
}
运行也不报错,debug跟踪也没有什么异常,搞不清楚怎么回事

解决方案 »

  1.   


    测试方法:package com.cstp.folder;import java.io.File;public class CreateDir {public void mkdir(String mkdirName)
    {
          try
          {
              File dirFile = new File(mkdirName);
              boolean bFile   = dirFile.exists();
              if( bFile == true )
              {
                 System.out.println("The folder exists.");
              }
              else
              {
                 System.out.println("The folder do not exist,now trying to create a one...");             bFile = dirFile.mkdir();
                 if( bFile == true )
                 {
                    System.out.println("Create successfully!");
                    System.out.println("创建文件夹");
                 }
                 else
                 {
                     System.out.println("Disable to make the folder,please check the disk is full or not.");
                     System.out.println(" 文件夹创建失败,请确认磁盘没有写保护并且空间足够");
                     System.exit(1);
                 }
              }
          }
          catch(Exception err)
          {
              System.err.println("ELS - Chart : 文件夹创建发生异常");
              err.printStackTrace();
          }
    }
    public static void main(String[]args){
       CreateDir app=new CreateDir ();
       app.mkdir("c:\\song7g\\gfgh");
      
    }}承揽:网站开发,SEO优化,毕业设计,OA,ERP,空间,域名,企业级管理系统  业务范围:[.net] [java]
      

  2.   

    在做debug的时候,能进入到theFile.mkdirs(); 这句话,但是就是没有创建,真是窝火!!!
      

  3.   

    换成request.getRealPath("/work/"+albumName)+")
      

  4.   

    改成:request.getContextPath()+"\\work\\"+albumName;
      

  5.   

    干脆用反射CreateDir得到绝对路径试下
      

  6.   

    你的文件应该被创建在你的tomcat所在盘符根目录下面。
    你将request.getContextPath()替换为request.getRealPath(".")就可以了。
      

  7.   


    你之前的文件都被创建在你的tomcat所在盘符根目录下面了,你自己找找吧你将request.getContextPath()替换为request.getRealPath(".")就可以了,结贴吧!