hyy9512

导航

文件5. 获取指定类型的文件

.jsp界面

 1 <form>
 2               <table>
 3           <tr>
 4               <td>输入文件夹路径:</td>
 5               <td><input type="text" name="filesPath" /></td>
 6           </tr>
 7           <tr>
 8               <td>输入文件的扩展名:</td>
 9               <td><input type="text" name="type" /></td>
10           </tr>
11           <tr>
12               <td colspan="2">
13                   <input type="button"  value="提交"/>
14               </td>
15           </tr>
16           <tr>
17               <table id="file">
18               
19               </table>
20           </tr>
21       </table>
22       </form>

.js代码

 1 $().ready(function(){
 2     $("input[type='button']").click(function(){
 3         var filesPath=$("input[name='filesPath'").val();
 4         var type=$("input[name='type']").val();
 5         var xmlhttp=getXmlhttp();
 6         var data={"filesPath":filesPath,"type":type};
 7         xmlhttp.onreadystatechange=function() {
 8             if(xmlhttp.readyState==4 && xmlhttp.status==200) {
 9                 var files=eval("("+xmlhttp.responseText+")");
10                 var table=$("#file");
11                 table.empty();
12                 var content="<tr><td>文件名</td><td>文件长度</td><td>修改日期</td></tr>";
13                 for(i in files) {
14                      content+="<tr><td>"+files[i][0]+"</td><td>"+files[i][1]+"</td><td>"+files[i][2]+"</td></tr>";
15                 }
16                 table.html(content);
17             }
18         };
19         xmlhttp.open("POST","showFile","true");
20         xmlhttp.setRequestHeader("Content-Type","application/json");
21         xmlhttp.send(JSON.stringify(data));
22     });
23 });
24 //获取xmlHttp
25 function getXmlhttp() {
26     var xmlhttp;
27     if(window.XMLHttpRequest) {
28         xmlhttp=new XMLHttpRequest;
29     } else {
30         xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
31     }
32     return xmlhttp;
33 }

servlet层ShowSomeType.java

 1 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
 2             throws ServletException, IOException {
 3         req.setCharacterEncoding("utf-8");
 4         resp.setCharacterEncoding("utf-8");
 5         resp.setContentType("text/html;charset=utf-8");
 6         PrintWriter out=resp.getWriter();
 7         JSONObject data=getJsonObject(req);
 8         String filesPath=data.getString("filesPath");
 9         String type=data.getString("type");
10         File[] files=null;
11         if(filesPath!=null && type!=null) {
12             CustomFilter fileFilter=new CustomFilter();
13             fileFilter.setExtentName(type);
14             File file=new File(filesPath);
15             if(file.isDirectory()) {
16                 files=file.listFiles(fileFilter);
17             }
18         }
19         if(files!=null) {
20             List<Object[]> fileList=new ArrayList<Object[]>();
21             for(File f:files) {
22                 Object[] file={f.getName(),f.length(),new Date(f.lastModified()).toLocaleString()};
23                 fileList.add(file);
24             }
25             JSONArray jsonFile=JSONArray.fromObject(fileList);
26             out.write(jsonFile.toString());
27         }
28     }
29     
30     private JSONObject getJsonObject(HttpServletRequest req) {
31         StringBuffer json=new StringBuffer();
32         String lineString=null;
33         BufferedReader reader;
34         JSONObject data=null;
35         try {
36             reader = req.getReader();
37             while((lineString=reader.readLine())!=null) {
38                 json.append(lineString);
39             }
40              data=JSONObject.fromObject(json.toString());
41         } catch (IOException e) {
42             e.printStackTrace();
43         }
44         return data;
45     }

 

posted on 2017-12-16 19:36  hyy9512  阅读(287)  评论(0编辑  收藏  举报