簡體   English   中英

需要幫助從 Java JSP 運行遞歸觸摸

[英]Need help running a recursive touch from 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);
}


%>

您真的要觸摸/home/abcdefg下的文件嗎? 我可以想象你想觸摸/home/ss/public_html/ss下的所有文件。 如果是這種情況,您必須更改 find 命令:

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

嘗試分離命令 arguments:

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

一直沿着相同的路徑 - 我想出了答案,它接近你們發布的內容:

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

謝謝大家的快速回復!!

丹尼爾

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM