.AJAX实现删除文件(服务器用jsp代替)

http://chaoji-liangbin.blog.163.com/blog/static/25239212201061192240909/

 

对应的js代码:

 function deleteSelectItem(oSelect)
  {
   var delItem = "";
       for(var i=0; i<oSelect.options.length; i++)
       {
        if(i>=0 && i<=oSelect.options.length-1 && oSelect.options[i].selected)
        {
         delItem = oSelect.options[i].value;
         oSelect.options[i] = null; 
              i --;
             }
       }
      

      var http_request = false;
      send_request("DeleteFile.jsp?path="+delItem);
      
       
      //向服务器发起XMLHTTP请求
      function send_request(url)
      {  
       //初始化,指定处理函数,发送请求的函数
       http_request = false;
       
      
 
//开始初始化XMLHTTPRequest对象
        if(window.ActiveXObject)
       { //IE浏览器
        try{
         http_request = new ActiveXObject('Msxm12.XMLHTTP');
         }catch(e)
         {
         try{
          http_request = new ActiveXObject("Microsoft.XMLHTTP");
         }catch(e)
         {
            alert(e);
         }
         } 
            //alert("IE");
        }else if(window.XMLHttpRequest)
         { //Mozilla浏览器
          http_request = new XMLHttpRequest();
         if(http_request.overrideMimeType)
         {  //设置MIME类别
          http_request.overrideMimeType('text/xml');
         }
        }  
       if(!http_request){ //有异常,创建对象实例失败
        window.alert("不能创建XMLHTTPRequest对象");
        return false;
       }
       
       http_request.onreadystatechange = processRequest;  //这里只是指向方法,并未执行方法
 
 
      url = encodeURI(url);
      url = encodeURI(url);//两次,很关键
 
       //确定发送请求的方式和URL以及是否执行以下代码
       http_request.open("POST",url,true);
      http_request.setrequestheader("Content-Type","application/x-www-form-urlencoded");
       http_request.send(null);
      }
      
      //处理返回信息的函数
      function processRequest(){
       if(http_request.readyState == 4){  //判断对象状态
        if(http_request.status==200){  //信息已经成功返回
         var returnStr = http_request.responseText;
          document.getElementById("feedback").innerHTML=returnStr;
        }else{
         alert("你所请求的页面有异常");
        }
       }
      }
  }
  

DeleteFile.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<%@page import="java.io.File"%>
<%@page import="java.net.URLDecoder"%>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">

<%
 response.setContentType("text/html");
 response.setCharacterEncoding("UTF-8");

 String filepath = URLDecoder.decode(request.getParameter("path"),"UTF-8");
 try {
  File f = new File(filepath);
  if (f.isFile()) {
   f.delete();
   out.println("删除 " + f.getName() + " 成功");
  } else {
   out.println("ERROR,选择的不是一个有效的文件");
  }
 } catch (Exception e) {
  out.println("ERROR,文件读取异常!");
 }
%>

 

FileConfig.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<%@page import="java.io.File"%>
<%!File[] configfiles;%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <title>配置文件管理</title>
  <link href="../images/common.css" rel="stylesheet" type="text/css"></link>
 <!--   <link href="../images/index.css" rel="stylesheet" type="text/css" />-->
 <style type="text/css">
 <!--
  ol,ul  ,li {
 list-style: none;
}
select {
 width: 200px;
}
img {
 border: 0;
}

.clear {
 clear: both;
 height: 1px;
 margin-top: -1px;
 width: 100%;
}
/*此上面代码是初始CSS模板,下面是新写CSS布局框架代码*/
#header  ,#centers  ,#footer {
 width: 100%;
 margin: 0 auto;
 clear: both;
 font-size: 18px;
 font-weight: bold;
}

#header {
 height: 20px;
 line-height: 32px;
}

#centers {
 padding: 8px 0;
 height: 160px;
}

#footer {
}

#centers .c_left {
 float: left;
 width: 200px;
 background: #F7F7F7;
 margin: 0,0,0,20%;
}

#centers .c_right {
 float: left;
 width: 80px;
 margin: 0,0,0,2px;
 padding: 0px;
}
.button {
 border: 1px solid black;
 width: 80px;
}
-->
 </style>
  <script type="text/javascript" src="../js/main.js"></script>
 </head>

 <body>
  <%
   try {
    String filepath = application.getRealPath("/WEB-INF/classes/");
    if (filepath.indexOf("//.//") != -1) {
     int i = filepath.indexOf("//.//");
     filepath = filepath.substring(0, i)
       + filepath.substring(i + 2, filepath.length());
    }
    //System.out.println("配置文件路径:"+filepath);
    File configdir = new File(filepath + "/config");
    if (configdir.isDirectory()) {
     configfiles = configdir.listFiles();
    }
    //System.out.println("configdir:" + configdir.getAbsolutePath());

   } catch (Exception e) {
    e.printStackTrace();
   }
  %>
  <form action="index.jsp" method="post">
   <div id="header">
    文件管理
   </div>
   <div id="centers">
    <div class="c_left">
     <SELECT size="10" id="select" name="select" multiple="single">
      <%
       if (configfiles != null) {
        for (File xmlfile : configfiles) {
      %>
      <option value="<%=xmlfile.getAbsolutePath()%>">
       <%=xmlfile.getName()%>
      </option>
      <%
       }
       }
      %>
     </SELECT>
    </div>
    <div class="c_right">
     <INPUT class="button" type="submit" value=" 打 开 " > &nbsp;
     <INPUT class="button" type="button" value=" 删 除 " onClick="deleteSelectItem(document.all.select);">
    </div>
   </div>
   <div id="footer">
    <label id="feedback"></label>
   </div>
  </form>
 </body>
</html>

 

 

为了支持 send_request("DeleteFile.jsp?path="+delItem);参数可以传中文以及特殊字符(""<>等),有两个地方需要注意,一是  url = encodeURI(url);
      url = encodeURI(url);//两次,很关键

两次encodeURI很重要

二是.在发送到得jsp页面里

response.setContentType("text/html");
 response.setCharacterEncoding("UTF-8");

 String filepath = URLDecoder.decode(request.getParameter("path"),"UTF-8");
这三句话也是非常重要的,得到的字符串才是原始的字符串,连中文和特殊字符都可以传,什么都可以传。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值