您正在使用weblogic服务器上传文件。要上载的文件的临时文件将在文件夹E:\WL\user_projects\domains\AMS_domain\servers\AdminServer\tmp\_WL_user\GenesisDenmark\3m840e\public
上创建。
如果我上传test.zip,则会将其复制到名为test.tmp的上述位置。我想以程序方式删除它。 我正在使用struts 1.1。
如果有人做过类似的事情。请帮忙。
谢谢!
答案 0 :(得分: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文件在点击discard按钮时所做的更新,它将还原更改并删除上传的xml文件。