开发者

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()开发者_StackOverflow) != 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜