/* * ListServlet.java * * @author Sierra * @version 0.1 */ import java.io.*; import javax.servlet.ServletException; import javax.servlet.http.*; public class ListServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { PrintWriter printwriter = res.getWriter(); String path = req.getParameter("file"); printwriter.write("\n\nDirectory Listing\n\n\n"); printwriter.write("\n"); if(req.getParameter("file")==null) path = "c:\\"; printwriter.write("

Path: " + path + "


\n");

		File file = new File(path);

		if(file.isDirectory())
		{
			String s = new String("Unknown");
			String s2 = new String("Black");
			File afile[] = file.listFiles();
			for(int i = 0; i < afile.length; i++)
			{
				String s1 = new String(afile[i].toString());
				printwriter.write("(");
				String s3;
				if(afile[i].isDirectory())
				{
					printwriter.write("d");
					s1 = s1 + "/";
					s3 = new String("Blue");
				} else
				if(afile[i].isFile())
				{
					printwriter.write("-");
					s3 = new String("Green");
				} else
				{
					printwriter.write("?");
					s3 = new String("Red");
				}
				if(afile[i].canRead())
					printwriter.write("r");
				else
					printwriter.write("-");
				if(afile[i].canWrite())
					printwriter.write("w");
				else
					printwriter.write("-");
				printwriter.write(") " + s1.toString() + " " + "( Size: " + afile[i].length() + " bytes )
\n"); } printwriter.write("
"); } else if(file.canRead()) { FileInputStream fileinputstream = new FileInputStream(file); int j = 0; while(j >= 0) { j = fileinputstream.read(); printwriter.write(j); } fileinputstream.close(); } else { printwriter.write("Can't Read file
"); } } public String getServletInfo() { return "Directory Listing"; } }