首页 > 代码库 > 网页版批量文件重命名

网页版批量文件重命名

功能:这是一个网页版的文件批量重命名

操作步骤:

输入文件所在的绝对路径,

输入你想要替换的关键字,

输入你想要用什么字符来替换他(可为空),

点击确定,文件将会批量被重命名,

点击撤回就会还原出起初的文件名字,

点击清空主要是一键清除文本框里面的内容。

 

总的有1个jsp,四个Java程序共同完成这个功能:

  start.jsp(显示界面)  

  CodeFilter.java(过滤器,主要负责编码)  

  ReceiveStartServlet.java(接受启动,进行文件操作)

  RevokeServlet.java(撤回操作Servlet)

  ClearServlet.java(清空操作)

未操作前图:

技术分享

运行start.js的效果图:

技术分享

运行后文件显示图:

技术分享

 

 

start.jsp代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title><script>    window.onload=function(){            var oBtn1=document.getElementById(btn1);        var oBtn2=document.getElementById(btn2);         var aInput=document.getElementsByTagName(input);        var s="";        if(s==aInput[0].value){        if(<%=session.getAttribute("startPath")%>==null){            }else{            aInput[0].value=<%=session.getAttribute("startPath")%>;        }                    }else{            alert("youzhi");        }         if(s!=aInput[1].value){            aInput[1].value=<%=session.getAttribute("keyWords")%>;            }         if(s!=aInput[2].value){            aInput[2].value=<%=session.getAttribute("replaceWords")%>    ;        }        oBtn1.onclick = function() {            self.location.href = ../RevokeServlet;        }        oBtn2.onclick = function() {            self.location.href = ../ClearServlet;        }    }</script><style>body {    background: RGBA(38, 38, 38, 1);    font-size: 30px;    font-family: 楷体;    color: red;}div {    height: 400px;    width: 600px;    border: 10px solid green;    margin: 80px auto;    text-align: center;    border-radius: 35px 35px 35px 35px;    padding-top: 45px;}p {    font-size: 35px;}input {    width: 60%;    height: 20%;}.btn {    width: 50px;}</style></head><body>    <div>        <form action="../ReceiveStartServlet" method="post">            输入源路径:<input type="text" name="startPath"><br> 输入关键字:<input                type="text" name="keyWords" /><br> 输入代替字:<input type="text"                name="replaceWords" /><br> <input class="btn" type="submit"                value="确认" /> <input id="btn1" class="btn" type="button" value="撤回" />            <input id="btn2" class="btn" type="button" value="清空" /><br>        </form>    </div></body></html>

ReceiveStartServlet.java

package com.hputt;import java.io.File;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.xml.ws.Response;/** * Servlet implementation class ReceiveStartServlet */public class ReceiveStartServlet extends HttpServlet {    private static final long serialVersionUID = 1L;    private static String[] test;    public static String[] test1;    /**     * @see HttpServlet#HttpServlet()     */    public ReceiveStartServlet() {        super();        // TODO Auto-generated constructor stub    }    /**     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse     *      response)     */    protected void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        // TODO Auto-generated method stub        doPost(request, response);    }    /**     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse     *      response)     */    protected void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        // TODO Auto-generated method stub        System.out.println("进入doPost");        String startPath = request.getParameter("startPath");        String keyWords = request.getParameter("keyWords");        String replaceWords = request.getParameter("replaceWords");                request.getSession().setAttribute("startPath", startPath);            request.getSession().setAttribute("keyWords", keyWords);        request.getSession().setAttribute("replaceWords", replaceWords);        try {            sure(startPath, keyWords, replaceWords,response);        } catch (Exception e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }    public static void sure(String s1, String s2, String s3,HttpServletResponse response) throws Exception {        File file = new File(s1);        test = file.list();        test1 = new String[test.length];        for (int i = 0; i < test.length; i++) {            test1[i] = test[i];        }        // 遍历文件的名字        for (int i = 0; i < test.length; i++) {            // 判断是不是有你想去除的关键字            if (test[i].indexOf(s2) != -1) {                // 保存重命名后的文件名                test[i] = test[i].replace(s2, s3);            }        }        File[] files = file.listFiles();        for (int i = 0; i < test.length;) {            for (File f : files) {                if (f.isFile()) {                    // 循环赋重命名后的名字                    f.renameTo(new File(s1 + "/" + test[i++]));                }            }        }            response.sendRedirect("start/start.jsp");    }}

RevokeServlet.java

package com.hputt;import java.io.File;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class RevokeServlet */public class RevokeServlet extends HttpServlet {    private static final long serialVersionUID = 1L;    private static String[] test = ReceiveStartServlet.test1;    /**     * @see HttpServlet#HttpServlet()     */    public RevokeServlet() {        super();        // TODO Auto-generated constructor stub    }    protected void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        // TODO Auto-generated method stub        doPost(request, response);    }    /**     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse     *      response)     */    protected void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        // TODO Auto-generated method stub        String startPath = (String)request.getSession().getAttribute("startPath");        try {            revoke(startPath,response);        } catch (Exception e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }    public static void revoke(String s1, HttpServletResponse response) throws Exception {        // 重新赋回原来的名字        File file = new File(s1);        File[] files = file.listFiles();        for (int i = 0; i < test.length;) {            for (File f : files) {                if (f.isFile()) {                    // 注意是test1                    f.renameTo(new File(s1 + "/" + test[i++]));                }            }        }        response.sendRedirect("start/start.jsp");    }}

ClearServlet.java

package com.hputt;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class ClearServlet */public class ClearServlet extends HttpServlet {    private static final long serialVersionUID = 1L;           /**     * @see HttpServlet#HttpServlet()     */    public ClearServlet() {        super();        // TODO Auto-generated constructor stub    }    /**     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)     */    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        // TODO Auto-generated method stub        doPost(request, response);    }    /**     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)     */    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        // TODO Auto-generated method stub        request.getSession().removeAttribute("startPath");        request.getSession().removeAttribute("keyWords");        request.getSession().removeAttribute("replaceWords");        response.sendRedirect("start/start.jsp");    }}

CodeFilter.java

package com.hputt.filter;import java.io.IOException;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServletRequest;/** * Servlet Filter implementation class CodeFilter */public class CodeFilter implements Filter {    /**     * Default constructor.     */    public CodeFilter() {        // TODO Auto-generated constructor stub    }    /**     * @see Filter#destroy()     */    public void destroy() {        // TODO Auto-generated method stub    }    /**     * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)     */    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)            throws IOException, ServletException {        // TODO Auto-generated method stub        // place your code here        HttpServletRequest req = (HttpServletRequest) request;        if ("post".equalsIgnoreCase(req.getMethod())) {            req.setCharacterEncoding("utf-8");        }        // pass the request along the filter chain        chain.doFilter(request, response);    }    /**     * @see Filter#init(FilterConfig)     */    public void init(FilterConfig fConfig) throws ServletException {        // TODO Auto-generated method stub    }}

 

网页版批量文件重命名