Use the Java program to get a procedure before the absolute path, encounter such a problem, want to use a relative path to delete a file (actually exist), always delete. It's really anxious, and finally let me spend a big The strength is to solve it, the problem does not prevent you to talk to you, if you encounter such problems, you don't have to charge again! This is this: my Tomcat is put on the C drive, and my virtual directory is set under E: / WORK, I have a picture in E: / Work / Test / Image, Test.gif I want to pass the program Delete it, but his absolute path is uncertain (in order to take into account the transplant after the program, the absolute path is uncertain). Suppose the del.jsp files under E: / WORK / TEST, as if you can delete: <% @ page contenttype = "text / html; charset = GBK "errorpage =" "%> <% request.setcharacterencoding (" gbk ");%> <% @ page language =" java "import =" java.sql. * "Import =" java.util. * "Import = "java.text. *" Import = "java.io. *"%>
path_test.java code is as follows: package pathtest; import java.io *; import javax.servlet *; import javax.servlet.jsp.PageContext; // import PageContext class, do not forget public class path_test {protected ServletContext m_application;.. private boolean m_denyPhysicalPath; public path_test () {} public final void initialize (PageContext pageContext) throws ServletException {m_application = pageContext.getServletContext ();} public String getPhysicalPath (String filePathName, int option) throws IOException {String path = new String () ; String fileName = new String (); String fileSeparator = new String (); boolean isPhysical = false; fileSeparator = System.getProperty ( "file.separator"); if (filePathName == null) throw new IllegalArgumentException ( "There is no Specified Destination File (1140). "); IF (FilePathname.Equals (")) "))" "There IS No Specified Destination File (1140)); if (filepathname.lastindexof (" // ")> = 0) {path = filepathname.substring (0, filepathname.lastindexof ("//")); filename = filepathname.substring (Fil EpathName.lastIndexof ("//") 1);} if (filepathname.lastindexof ("/")> = 0) {path = filepathname.substring (0, filepathname.lastindexof ("/")); filename = filepathname .substring ("/") 1);} path = path.length ()! = 0? path: "/";java.io.file physicalPath = new java.io.file (path); IF (PhysicalPath.exists ()) isphysical = true; if (option == 0) {if (isvirtual (path)) {path = m_application.getRealPath (path); if (path.Endswith (fileseparator)) PATH = PATH FileName; elsepath =
String.valueOf ((new StringBuffer (String.valueOf (path))) append (fileSeparator) .append (fileName).); Return path;} if (isPhysical) {if (m_denyPhysicalPath) throw new IllegalArgumentException ( "Physical path is denied (1125). "); Elsereturn Filepathname;} else {throw new IllegalargumentException (" this path does not exist (1135). ");}} F (option == 1) {if (isvirtual (path) {path = m_application.getRealPath (path); if (path.endsWith (fileSeparator)) path = path fileName;. elsepath = String.valueOf ((new StringBuffer (String.valueOf (path))) append (fileSeparator) .append (fileName) );} IF (isphysical) Throw new IllegalargumentException ("THIS NOT A Virtual Path."); Elsethrow New IllegaLutException ("this path does not exist (1135).");} if (option == 2 ) {if (isPhysical) if (m_denyPhysicalPath) throw new IllegalArgumentException ( "Physical path is denied (1125)."); elsereturn filePathName; if (isVirtual (path)) throw new IllegalArgumentException ( "The path is not a physical path." ElSethrow New IllegalarGumeTexcep TION ("This path does not exist (1135).");} else {return null;}}} private bolean isvirtual (string pathname) // Detailed whether it is a virtual path {if (m_application.getRealPath (Pathname)! = null) {java.io.file VirtualFile = new java.io.file (m_application.getRealPath (Pathname)); returnifile.exists ();} else {return false;}}} After compiling Path_Test.java, get Pathtest, There is a PATH_TEST.CLASS class, put the entire package to the Classes of the virtual directory, and then change the DEL.JSP file to the following procedure, everything is OK! <% @ page contenttype = "text / html; charset = GBK" errorpage = ""%> <% request.setcharacterencoding ("gbk");%> <% @
Page language = "java" import = "java.sql. *" Import = "java.util. *" import = "java.text. *" IMPORT = "java.io. *"%>