简体   繁体   中英

Need help running a recursive touch from Java JSP

Is there any way in Java to use the exec method to perform a recursive touch?

The goal is to setup a simple webpage that when reloaded will touch the dir for the site so that design can guarentee caching is no longer happening. Any help please!!!

Here is what I have so far, and not working in my jsp:

<%@ page import="java.io.BufferedReader,java.io.File,java.io.FileWriter, java.io.IOException, java.io.InputStreamReader, java.util.Map" %>

<%


String s = null;
// system command to run
String cmd = "find /home/abcdefg/ -exec touch {} \\;";
// set the working directory for the OS command processor
File workDir = new File("/home/ss/public_html/ss");

try {
Process p = Runtime.getRuntime().exec(cmd, null, workDir);
int i = p.waitFor();
if (i == 0){
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
// read the output from the command
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
}
else {
BufferedReader stdErr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
// read the output from the command
while ((s = stdErr.readLine()) != null) {
System.out.println(s);
}

}
}
catch (Exception e) {
System.out.println(e);
}


%>

Do you really want to touch the files under /home/abcdefg ? I could imagine that you want to touch all files under /home/ss/public_html/ss . If that's the case you have to change the find command:

String cmd = "find /home/ss/public_html/ss -exec touch {} \\;"

Try to separate command arguments:

// system command to run
String[] cmd = {"find","/home/abcdefg/","-exec","touch","{}",";"};

All along the same path - I figured out the answer and it is close to what you guys posted:

<% String[] cmd = {"/bin/sh", "-c", "cd /xx/xx/xx/xx; find. -exec touch {} \;"}; Process p = Runtime.getRuntime().exec( cmd ); %>

Thank you guys for the quick response!!

Daniel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM