String realpath=request.getRealPath("");
-->
String realpath=request.getRealPath(".");

解决方案 »

  1.   

    这是我写的一个删除文件或目录的方法,你参考一下吧
    public void deletefile(String delpath) throws FileNotFoundException,IOException {
    File file=new File(delpath);
    if(!file.isDirectory()) {
      file.delete();
      return;
    } else if(file.isDirectory()) {
      String[] filelist=file.list();
      for(int i=0;i<filelist.length;i++) {
        File delfile=new File(delpath+"/"+filelist[i]);
        if(!delfile.isDirectory())
          delfile.delete();
        else if(delfile.isDirectory())
          deletefile(filelist[i]);
      }
      file.delete();
    }
    System.exit(0);
    }
      

  2.   

    sorry,try
    File f=new File(realpath+"\\bbsupload\\70.gif");
      

  3.   

    不是呀,我用GETREALPATH得出的路径是确实存在的
      

  4.   

    你不是在本地机上操作的吧!所以的你的路径很可能会与(如tomcat)的默认路径有关。上面讲的都是本地机的操作,你的问题很可能是这样。
      

  5.   

    我在家里只有JDK,所以用下面的试了一下,一点问题都没有,是不是getRealPath()这个方法有什么问题,比如说他返回的String在最后面已经有"\\"了,或者有几个"空格",这样的话,你构造File对象代表的文件也许就会真的不存在。如果,你输出的路径确实没有问题,那么你可以使用
        File f=new File(realpath.trim()+"\\bbsupload\\","70.gif");这样就可以把"空格去掉"。
              
    import java.io.*;public class DelFile {
    public static void main(String[] args) {
    String realpath="d:\\project\\java";
    File f=new File(realpath+"\\test\\","11.txt");
    System.out.println(realpath);
    if (f.exists()) {
    f.delete();
    } else {
    System.out.println("no exiset");
    }
    }
    }
      

  6.   

    FT呀,楼上的程序我可以运行删除,但我的JSP就不行呀
      

  7.   

    这是Java文档里面关于getRealPath地描述,我想最终应该还是构造File对象的String格式由问题,你可以先把文件名的字符串合成,然后打印出来,再看应该如何做修改。比如,可能getRealPath返回的是 d:\project\java 或者 d:\project\java\ 再或者 d:\\project\\java, 这样都会造成格式不对。public String getRealPath(String path)Returns a String containing the real path for a given virtual path. For example, the path "/index.html" returns the absolute file path on the server's filesystem would be served by a request for "http://host/contextPath/index.html", where contextPath is the context path of this ServletContext.. The real path returned will be in a form appropriate to the computer and operating system on which the servlet container is running, including the proper path separators. This method returns null if the servlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war archive). 
    Parameters:
    path - a String specifying a virtual path 
    Returns:
    a String specifying the real path, or null if the translation cannot be performed
      

  8.   

    我的是JDK 1。3。1,TOMCAT 4。1,
    我在你的程序中,改为
    String realpath="d:\\jakarta-tomcat-4.0.3\\webapps\\inet\\bbs";
    File f=new File(realpath+"\\bbsupload\\","95trans.gif");这样就无法删除文件,说不存在
    会不会是因为我将你的DELFILE。JAVA也放在
    d:\\jakarta-tomcat-4.0.3\\webapps\\inet\\bbs
    下运行造成的?
      

  9.   

    但是我删除其他目录下的就可以(不放在TOMCAT目录下的),到底是什么问题?
      

  10.   

    String realpath=request.getRealPath("");
      File f=new File(realpath+"\\bbsupload\\","70.gif");
    改为
    String realpath=request.getRealPath("");
      File f=new File(realpath+"\\bbsupload\\"+"70.gif");
    这样就不会出错了。
      

  11.   

    还是不行呀,反正一放在TOMCAT下的子目录及孙目录都不行
      

  12.   

    String path = (System.getPropety("user.dir")+"/bbsupload/.../").replace('\\','/');
    File f = new File(path);
    f.delete;或者使用相对目录试试。我用weblogic就能删除它目录下的文件。
      

  13.   

    sorry :
    String path = (System.getPropety("user.dir")+"/bbsupload/.../abc.gif").replace当然,你的文件要在非使用状态