String filePath="c:/支出证明单.xls";
filePath=filePath.toString();
java.io.File myDelFile=new java.io.File(filePath);
myDelFile.delete();

解决方案 »

  1.   

    你用上面的程序已经可以了
    filePath就是你的文件的绝对路径
      

  2.   

    To: leshui(大象无形)
    我用了你上面的程序可以正确删除硬盘上指定目录的文件,但是我这里始终有问题(路径是由数据库返回的值组成),我用rs.getString直接取值不行,用session传递值也不行...帮我看看好吗?十分感谢!!<%@ page contentType="text/html; charset=gb2312" %>
    <%@ page import = "java.sql.*" %>
    <%!
    Connection con;
    Statement stmt;
    ResultSet rs;
    %>
    <%
    try {
    Class.forName("org.gjt.mm.mysql.Driver");
    con = DriverManager.getConnection ("jdbc:mysql://localhost/resource_db?user=root&password=");
    stmt = con.createStatement();
    String i=request.getParameter("checkbox");
    rs = stmt.executeQuery("select * from menu_kejian where ID='"+i+"'");while (rs.next()) {
    String g=rs.getString("Grade_ID");
    session.setAttribute("Grade",g);String c=rs.getString("Course_ID");
    session.setAttribute("Course",c);String t=rs.getString("Type_ID");
    session.setAttribute("Type",t);String f=rs.getString("File");
    session.setAttribute("File",f);
    }stmt.executeUpdate
      ("delete from menu_kejian where ID='"+i+"'");
    %>删除记录成功!
    <a href=display.jsp>查看</a>
    <%
    }catch (ClassNotFoundException e) {
    out.println("Couldn't load database driver: " + e.getMessage());
    }
    catch (SQLException e) {
    out.println("SQLException caught: " + e.getMessage());
    }
    finally {
    try {
    if (con != null) con.close();
    }
    catch (SQLException ignored) {}
    }rs.close();
    stmt.close();
    con.close();String g1=(String)session.getAttribute("Grade");
    String c1=(String)session.getAttribute("Course");
    String t1=(String)session.getAttribute("Type");
    String f1=(String)session.getAttribute("File");String Path=g1+"/"+c1+"/"+t1+"/"+f1+";
    String filePath="http://localhost:8080/examples/upload/+path";filePath=filePath.toString();
    java.io.File myDelFile=new java.io.File(filePath);
    myDelFile.delete();session.removeAttribute("Grade");
    session.removeAttribute("Course");
    session.removeAttribute("Type");
    session.removeAttribute("File");
    %>
    删除文件成功!
      

  3.   

    filePath应该为本地绝对路径吧?如果包含中文名,可能需要编码转换啊^_^
      

  4.   

    filePath为本地绝对路径的话我就明白了:p
    String Path=g1+"/"+c1+"/"+t1+"/"+f1+";
    String filePath="http://localhost:8080/examples/upload/+path";
    改成
    String filePath="C:/Program Files/Apache Group/Tomcat 4.1/webapps/examples/upload/"+g1+"/"+c1+"/"+t1+"/"+f1+"";
    我刚测试,已经通过了,但还是要用session来传递变量,直接返回数据库的值就是不行..不解....