需要从 Java JSP 运行递归触摸的帮助
Java中有没有办法使用exec方法来执行递归触摸?
目标是设置一个简单的网页,重新加载时将触及该网站的目录,以便设计可以保证不再发生缓存。请任何帮助!
这是我到目前为止所拥有的,但没有在我的 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);
}
%>
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);
}
%>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您真的想触摸
/home/abcdefg
下的文件吗?我可以想象您想要触摸/home/ss/public_html/ss
下的所有文件。如果是这种情况,您必须更改 find 命令: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:尝试分隔命令参数:
Try to separate command arguments:
沿着同一条路 - 我找到了答案,它与你们发布的内容很接近:
<%
String[] cmd = {"/bin/sh", "-c", "cd /xx/xx/xx/xx; find . -exec touch {} \;"};
进程 p = Runtime.getRuntime().exec( cmd );
%>
谢谢你们的快速回复!
丹尼尔
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