File rename in jsp

Discussions

Web tier: servlets, JSP, Web frameworks: File rename in jsp

  1. File rename in jsp (11 messages)

    Below is the code I am using to rename a file. I am running IIS 4.0 and JRun Server. The permissions on the directory are set to write by the web server account and the file to be copied is not an archive file. These are the only things I can think of that would prevent this code from working.

    String s=("input.txt");
    String d=("copy.txt");
    File f1 = new File(s);
    File f2 = new File(d);
    boolean b;
    b=f1.renameTo(f2);
    if (b)
    out.println("Sucessfully Transfered!");
    else
    out.println("Not Transfered!");

    Thanks

    Threaded Messages (11)

  2. File rename in jsp[ Go to top ]

    Nancy,

    have you tried to put the .renameTo(File dest) method in a try {} block and catch any exceptions:

    1. SecurityException - If a security manager exists and its SecurityManager.checkWrite(java.io.FileDescriptor) method denies write access to both the old and new pathnames
    2. NullPointerException - If the parameter dest is null

    Nicky
  3. File rename in jsp[ Go to top ]

    I don't think I know how to properly utilize the catch feature. I've added the try and catch blocks, but I'm not sure where the errors are going. Thanks for your help.
  4. File rename in jsp[ Go to top ]

    The try {} catch() will actually serve what you trying to accomplish with the if statement, but it will be better and will provide with more info on what exactly is taking place. The boolean value will give you only:
    - true - in case of succesful renaming;
    - false - unsuccesful;

    if you do:

    try {
         boolean b;
         b=f1.renameTo(f2);
    } catch (SecurityException se) {
         System.out.println("SecurityException")
         ex.printStackTrace ();
    } catch (NullPointerException npe) {
         System.out.println("NullPointerException")
         npe.printStackTrace ();
    }

    this will give you a good idea of what is taking place, if an Exception isn't thrown then you know that it is working fine and you need to look for the problem in another place - paths, permissions and etc..
  5. File rename in jsp[ Go to top ]

    Ok. thanks. No exception was thrown.

    The directory is writable by the web server anonymous account. The input file is not an archive file. the jsp page is in the same directory as the file to be renamed. I just call the file name, not the path since they are in the same directory.

  6. File rename in jsp[ Go to top ]

    what is the error, if any that you are getting after compilation? I just tried your code in my environment and works fine.
  7. File rename in jsp[ Go to top ]

    I get no errors when I compile the jsp file.
  8. File rename in jsp[ Go to top ]

    I've gone back and added the full path to the file in case that was the problem, but that did not fix it.
  9. File rename in jsp[ Go to top ]

    Nancy,

    would you post the whole JSP file, so I could look at it?

    Nicky
  10. File rename in jsp[ Go to top ]

    <%@ page import="java.io.*" %>

    <html>
    <body>
                      
    <%
    try {
    String s=("d:\\\\production\\\\f2jsite\\\\test\\\\fileread
    testfileio.jsp");
    out.println(s);
    out.println("<br>");

    String d=("d:\\\\production\\\\f2jsite\\\\test\\\\fileread
    copy.txt");
    out.println(d);
    out.println("<br>");

    File f1 = new File(s);
    boolean read;
    read = f1.canRead();
    out.println(read);
    out.println("<br>");

    boolean write;
    write = f1.canWrite();
    out.println(write);
    out.println("<br>");

    File f2 = new File(d);

    boolean read2;

    String path;
    read2 = f2.canRead();
    path = f1.getAbsolutePath();
    out.println(read2);
    out.println("<br>");
    out.println(path);
    out.println("<br>");

    boolean write2;
    write2 = f2.canWrite();
    String path2;
    path2 = f2.getAbsolutePath();
    out.println(write2);
    out.println("<br>");
    out.println(path2);
    out.println("<br>");

    boolean b;
    b=f1.renameTo(f2);
    if (b)
    out.println("transferred");
    else
    out.println("arrrggggghhhhh");

    }

    catch (SecurityException se) {
    out.println("SecurityException");
    se.printStackTrace ();
    }
    catch (NullPointerException npe) {
    out.println("NullPointerException");
    npe.printStackTrace ();
    }
                      
    %>


    </body>
    </html>

    here's the output I get (http://www.fruit2juice.com/test/fileread/testcopy.jsp)

    d:\\production\\f2jsite\\test\\fileread\testfileio.jsp
    d:\\production\\f2jsite\\test\\fileread\copy.txt
    false
    false
    false
    d:\production\f2jsite\test\fileread\testfileio.jsp
    false
    d:\production\f2jsite\test\fileread\copy.txt
    arrrggggghhhhh
  11. File rename in jsp[ Go to top ]

    mmm. I got it working. It would appear that I did not have the file path correct. I appreciate all your help. I feel not so bright at the moment. But, I learned a fair amount about debugging. So, again, thanks.

  12. File rename in jsp[ Go to top ]

    What is the OS on the production server? Win or Unix?