GeeCON Prague 2014*
The moose likes Servlets and the fly likes Deleting a file from server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Deleting a file from server" Watch "Deleting a file from server" New topic
Author

Deleting a file from server

Mandy Smith
Ranch Hand

Joined: Jun 26, 2001
Posts: 62
I have successfully uploaded an image into the server, but now i want to have the option of deleting this image from the server. How to do this? Please help me.
Appreciate your response!
Thanks.
sanjay yermalkar
Greenhorn

Joined: Sep 19, 2001
Posts: 23
A platform dependent solution. This line will execute a unix/dos command:
Runtime.getRuntime().exec("rm <filename>");

Mandy Smith
Ranch Hand

Joined: Jun 26, 2001
Posts: 62
Hi sanjay,

Could you please be more specific, i did not follow.
exec () method takes a command as a String and where do i specify my filename to be deleted.
This line
Runtime.getRuntime().exec("rm <filename>");
just executes the specified string command in a separate process, but i want the uploaded file to be deleted from the server. How do i do this?
Appreciate any further help!
Thanks,

manoj bagul
Greenhorn

Joined: Sep 20, 2001
Posts: 11
Platform indepened solution is like this
File deleteFile = new File(<file Path> ) ;
// check if the file is present or not
if( deleteFile.exists() )
deleteFile.delete() ;
Be carefule is the file path is directory then the directory must empty before deleting.
sanjay yermalkar
Greenhorn

Joined: Sep 19, 2001
Posts: 23
I think this is definietly better than executing an Unix command.
I had a problem with Unix launching it's own process which gets stuck somewhere.
[This message has been edited by sanjay yermalkar (edited September 20, 2001).]
Mandy Smith
Ranch Hand

Joined: Jun 26, 2001
Posts: 62
File to be deleted is on my Weblogic server, but exists(), isFile() methods return false and is not deleted. Am i doing something wrong. This is what i am doing.
String path = "." + File.separator + "myserver" + File.separator + "public_html" + File.separator + "test" + File.separator + "images" + File.separator + "test.gif" ;
File deleteFile = new File(path);
// check if the file is present or not
if( deleteFile.exists() )
deleteFile.delete() ;
Could anyone please help me,
Thanks.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
You're using a relative path, which if I'm not mistaken, is interpreted relative to the directory where the server was started. To verify, check the value of deleteFile.getCanonicalPath(). You'll probably see that this is not the path that you really want.
------------------
Miftah Khan
- Sun Certified Programmer for the Java 2 Platform
- Sun Certified Web Component Developer for the J2EE Platform
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Try the following. Note this is still platform independent.

Hope this works.

------------------
Miftah Khan
- Sun Certified Programmer for the Java 2 Platform
- Sun Certified Web Component Developer for the J2EE Platform
Mandy Smith
Ranch Hand

Joined: Jun 26, 2001
Posts: 62
Thank you very much Khan. It works fine now.
 
GeeCON Prague 2014
 
subject: Deleting a file from server