0
我想寫一個servlet來搜索Tomcat服務器上的文件,它給出了文件的相對位置。servlet來搜索服務器
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String gettext = request.getParameter("name");
String set = gettext;
String s = null;
RequestDispatcher dispatcher = request.getRequestDispatcher("search.jsp");
request.setAttribute("text", gettext);
File root = new File("here i want to give localhost:8080/webappfoldername");
String fileName = set + ".png";
System.out.println(fileName);
try {
boolean recursive = true;
Collection files = FileUtils.listFiles(root, null, recursive);
for (Iterator iterator = files.iterator(); iterator.hasNext();) {
File file = (File) iterator.next();
if (file.getName().equals(fileName)) {
System.out.println(file.getAbsolutePath());
s = file.getAbsolutePath();
// System.out.println(s);
getServletContext().setAttribute("return", s);
// request.setAttribute("return" ,file.getAbsolutePath());
String testing = (String) getServletContext().getAttribute("return");
// System.out.println(testing);
request.getRequestDispatcher("/page.jsp").forward(request, response);
}
}
if (s == null) {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
System.out.println("no file found");
}
} catch (Exception e) {
e.printStackTrace();
}
}
你需要一個[爬蟲](http://stackoverflow.com/questions/2495289/what-is-a-good-java-web -crawler庫)。 –
問題是什麼? –
您是否知道'DefaultServlet'的'listing'屬性? – BalusC