1

您好,在使用 weblogic 服务器上传文件时。要上传的文件的临时文件正在文件夹中创建E:\WL\user_projects\domains\AMS_domain\servers\AdminServer\tmp\_WL_user\GenesisDenmark\3m840e\public

如果我上传 test.zip,那么它将被复制到上面的位置,名称为 test.tmp。我想以编程方式删除它。我正在使用struts 1.1。

如果有人做过类似的事情。请帮忙。

谢谢!

4

1 回答 1

0

可能是因为其他一些原因。你真的想以编程方式删除吗?我可能错了(如果是,请告诉我),但您可以使用正则表达式来匹配所有临时文件。但更好的方法是,如果每次上传任何文件时都使用文件名,搜索具有相同名称和 .tmp 扩展名的文件。用 File 对象表示该文件,如果该特定文件存在,请尝试以编程方式删除该文件。(在 struts 中,您应该在 Action 类中执行此操作,或者如果您将 spring/EJB 与 struts 一起使用,则应该在您的业务逻辑中执行此操作.) 我认为您需要执行以下步骤

 1. Fetch the uploaded file name(As I think it will be with extension)
 2. get a substring of this file name upto the last location of . (to
    get the file name without extension)
 3. append .tmp in that file name
 4. check the condition if that file exists in your uploading directory
 5. if so, delete the file

一些示例代码,希望对您有所帮助:

        //Get the servers upload directory real path name
          filepath = getServletContext().getRealPath("/")+"xml_upload";
          System.out.println("The file path is "+filepath);
          //create the upload folder if not exists
           File folder = new File(filepath);
           if(!folder.exists())
           {
            folder.mkdir();
           }


     MultipartRequest m=new MultipartRequest(req,filepath);
     ff=m.getFile("f");
     System.out.println("Full Path with file name:"+ff);
     //get file name...
     filename=ff.getName();
     System.out.println("file name is:"+filename);

      //now here you have to get the file name without extension and then append the extension .tmp
 // now let the file is represented by a File object say ff .....

   if(ff.exists())
          {
           System.out.println("File exist and it is going to delete.");
           ff.delete() ;
           }

对于我使用的一个 XML 上传应用程序,我使用了相同的方法,因为 xml 数据将更新并显示新值,如果用户想在单击丢弃按钮时丢弃 xml 文件所做的更新,它将恢复更改并删除上传的 xml文件。

于 2013-04-05T11:37:58.107 回答