Discussions

Web tier: servlets, JSP, Web frameworks: How to delete a File from web-server without using FTP

  1. Hi All
    Thanx for giving ur invaluable time to my question.

        I hv given a facility to a site user to upload some files..
    And an interface to web administrator to view all the files..
    I want to know how can i write a code in JSP which allow the web administrator to delete any of that file which is not of his/her interest through provided interface only.
                                            OR
    Is is possible to delete a particular file from web server without getting login into FTP account

    Thanx


  2. // choose the appropriate filepath
    // you can also use File.separator for your slashes
    // if you are building a dynamic path

    // win32,dos
    String filePath = "c:\\tomcat\\webapps\\myapp
    myfile.txt";

    // unix,linux,etc
    String filePath = "/usr/local/tomcat/webapps/myapp/myfile.txt";

    // code to delete file :p
    File f = new File(filePath);
    if(f.exists()) {
        f.delete();
    }
             
    // yes it's that easy...


    so if you already have a listing of files, simply provide a URL link to each file or a form listing and then have it go to your controller... so you can have:

    File f = new File(request.getParameter("filePath");

    or build a dynamic file path

    String filePath = getServletContext().getRealPath("") + File.separator + "WEB-INF" + File.separator + "uploads" + File.separator + request.getParameter("fileName");

    I think you get my drift... but the code to delete is simple...