hyy9512

导航

文件4. 获取文件信息

servlet层获取通过文件路径获取文件的相关信息

.jsp界面

 1   <form action="<%=basePath %>fileAction" method="get" >
 2         <table>
 3             <tr>
 4                 <td>输入文件路径</td>
 5                 <td><input type="text" name="filePath" /></td>
 6             </tr>
 7        <tr>
 8                 <td colspan="2"><input type="submit" value="查看"/></td>
 9             </tr>
10             <tr>
11                 <td colspan="2">文件属性:</td>
12             </tr>
13             <tr>
14                 <td colspan="2"><table id="fileContent"></table></td>
15             </tr>
16         </table>
17     </form>
18             

.js界面

 1 $("input[name='filePath']").blur(function(){
 2         var val=$("input[name='filePath']").val();
 3         if(val=='') {
 4             alert("源文件路径不能为空!");
 5         } else {
 6             var data={"method":"getFileInfo","fromPath":val};
 7             var xmlhttp=getXmlhttp();
 8             xmlhttp.onreadystatechange=function() {
 9                 if(xmlhttp.readyState==4 && xmlhttp.status==200) {
10                     var table=$("#fileContent");
11                     table.empty();
12                     var content="";
13                     alert(xmlhttp.responseText);
14                     var fileInfo=eval("("+xmlhttp.responseText+")");
15                     for(i in fileInfo) {
16                         content+="<tr><td>"+i+"</td><td>"+fileInfo[i]+"</td></tr>";
17                     }
18                     table.html(content);
19                 }
20             };
21             xmlhttp.open("POST","fileAction",true);
22             xmlhttp.setRequestHeader("Content-Type","application/json");
23             xmlhttp.send(JSON.stringify(data));
24         }
25     });

servlet层FileAction.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 //        第一步:解析json字符串得到json对象
 8         JSONObject data=this.getJsonObject(req);
 9 //        第二步:获取json对象中的键获得值
10         String method=data.getString("method");
11         if(method.equals("getFileInfo")) {
12             JSONObject message=new JSONObject();
13             String path=data.getString("fromPath");
14             File file=new File(path);
15             if(file.exists()) {
16                 Map<String,String> fileInfo=new HashMap<String, String>();
17                 if(!file.isFile()) {
18                     message.put("message", "该路径不是文件!");
19                     out.write(message.toString());
20                 } else {
21                     fileInfo.put("文件名称",file.getName());
22                     fileInfo.put("文件路径",file.getCanonicalPath());
23                     fileInfo.put("上级目录", file.getParentFile().getParent());
24                     fileInfo.put("隐藏", file.isHidden()?"隐藏":"显示");
25                     fileInfo.put("只读属性", file.canWrite()?"可写":"不可写");
26                     fileInfo.put("最后修改日期", new Date(file.lastModified()).toString());
27                     fileInfo.put("文件长度", String.format("%#,.2fk", file.length()/1024.0));
28                     JSONObject jsonFileInfo=JSONObject.fromObject(fileInfo);
29                     out.write(jsonFileInfo.toString());
30                 }
31             } else {
32                 message.put("message", "该文件不存在");
33                 out.write(message.toString());
34             }
35         }
36     }
37 
38 
39    private JSONObject getJsonObject(HttpServletRequest req) {
40         StringBuffer json=new StringBuffer();
41         String lineString=null;
42         BufferedReader reader;
43         JSONObject data=null;
44         try {
45             reader = req.getReader();
46             while((lineString=reader.readLine())!=null) {
47                 json.append(lineString);
48             }
49              data=JSONObject.fromObject(json.toString());
50         } catch (IOException e) {
51             e.printStackTrace();
52         }
53         return data;
54     }

 

 

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