tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Anne Milbert" <anne.milb...@freenet.de>
Subject AW: Howto delete a file with a servlet???
Date Wed, 07 Dec 2005 18:48:02 GMT
Yes, now it works. Thanks a lot.

regards, anne

-----Ursprungliche Nachricht-----
Von: Carl Olivier [mailto:carl.olivier@unysen.co.uk]
Gesendet: Mittwoch, 7. Dezember 2005 19:35
An: 'Tomcat Users List'
Betreff: RE: Howto delete a file with a servlet???


Hi.

Solution 2 - 1 is not a great idea!

Also, try something like:

String fileName = request.getParameter("Id") + ".xml"; File toDel = new
File("c:\\", fileName); //get some info out.println("File exists:" +
toDel.exists()); out.println("File is readOnly: " + !toDel.canWrite()); If
(toDel.exists() && toDel.canWrite()) {
	if (!toDel.delete()) {
		Thread.sleep(1000); //try get around file lock/release
issue? (? Stab in the dark maybe!)
		if (!toDel.delete() {
			out.println("Failed to delete file - with
delay/pause and retry");
			//could also tell it to delete on JVM exit - not
that useful with most web apps though
			toDel.deleteOnExit();
		}
	}
} else {
	out.println("Failed to delete file " + fileName + " - file does not
exist or is read only!"); }

You could also try and put the sleep and retry in a loop.

However, the main error in your code was that the datName reference when
creating a the File object was a string - when it should be a variable
reference!  i.e. new File("datename") should be new File(datname);

Hope that helps,

Regards,

Carl

-----Original Message-----
From: Anne Milbert [mailto:anne.milbert@freenet.de]
Sent: 07 December 2005 16:13
To: Tomcat Users List
Subject: Howto delete a file with a servlet???

Hi anyone,

I'd like to delete a file with a servlet. I tried it with the delete()
function and with the runtime.exec() function. What am I doing wrong?

Here's my code:
Solution 1:
[...]
String cmd = "del C:\\" + request.getParameter("Id") + "-" + eleno + ".xml";
Runtime runtime = Runtime.getRuntime(); Process process =
runtime.exec(datname) [...]

Solution 2:
[...]
String datname = "C:\\" + request.getParameter("Id") + "-" + eleno + ".xml";
boolean success = (new File("datname")).delete(); if (!success) {
	out.println("Couldn't delete file: " + datname); } [...]

Regards,
Anne


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message