日期:2014-05-17  浏览次数:20818 次

一个奇怪的servlet 页面递归问题 .
在我的台式机上一显示页面就报错,
可是在我的笔记本上就能显示页面,但 myeclipse 的控制台也报差不多的错,
不过笔记本上也不是总没事,最开始的时候一发布就能显示页面,过了一段时间再发布就不能显示了,只能删了再重新导入就又可以了,好奇怪哦 .

代码如下 :

Java code

public class DirList extends HttpServlet{

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        String filePath="c:\\";
        if(request.getParameter("filePath")!=null)filePath=request.getParameter("filePath");
        
        this.showDir(filePath,response.getWriter());
    }
    
    private void showDir(String filePath,PrintWriter write) throws IOException{

        File dirFile=new File(filePath);        
        File[] fileList=dirFile.listFiles();
                
        for(int i=0;i<fileList.length;i++){
            File fileTmp=fileList[i];
            if(fileTmp.isFile()){
                write.write(fileTmp.getName()+"<br>");
            }else{
                
                write.write("<span>+</span>"+fileTmp.getName()+"<br>");
                write.write("<div style='margin-left:10;border:1px red solid;'>");
                
                
                //就是下面这句,注释掉就不报错了

                //this.showDir(fileTmp.getAbsolutePath(), write); 
            
            }

        }
        write.write("</div>");
    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.doGet(request, response);
    }

}



------解决方案--------------------
for(int i=0;i<fileList.length;i++){

==》
for(int i=0;fileList != null && i< fileList.length;i++){
------解决方案--------------------
那就只能是 fileList == null

估计是该目录你无权访问,或者你所指定的filePath根本不是目录。

无权访问这个就没啥好招了。

是否是目录可以先判断下的:
fileTmp.isDirectory()