我希望下面的代碼適合你。首先你需要一個servlet來讀取服務器的文件結構;您可能需要編輯以下代碼才能根據需要進行修復;
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = { "/serverpath" })
public class ServerPath extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
String link = "<li><a href=\"serverpath?path=%s\">%s</a> <input type=\"button\" value=\"select\" onclick=\"window.opener.document.getElementById('path').value = '%s'; window.close();\" /></li>";
response.setContentType("text/html");
try {
String path = req.getParameter("path");
if (path == null) {
// take disk drives, for linux/is enough for me
response.getOutputStream().print(String.format(link, "/", "/", "/"));
} else {
File file = new File(path); //read clicked file path and its sub paths.
if (file.isDirectory()) {
File[] subDir = file.listFiles();
for (File file2 : subDir) {
if (file2.isDirectory()) {
response.getOutputStream().println(
String.format(link, file2.getAbsolutePath(), file2.getAbsolutePath(), file2.getAbsolutePath()));
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
html/jsp頁面的示例;
<input type="text" id="path" />
<input type="button" value="Select" onclick="window.open('serverpath', '', 'width=700,height=500,top=150,left=150,scrollbars=yes,location=no')">
文件選擇器顯示客戶端計算機的目錄。但是你想從服務器中選擇一條路徑。我可能在開發階段工作,但它不會在生產中工作。你確定? –
嗨@Yusuf,是的。請讓我知道如何去做。 – user3872094
你認爲:(你需要使用JavaScript和Java在一起是不容易。 –