首页 > 代码库 > 网页版批量提取目录下特定文件类型

网页版批量提取目录下特定文件类型

功能:

这是一个网页版的文件批量提取特定目录下的某种类型的文件的功能。

初始化页面文本框中值为空,当输入完成确认后,文本框中数据不会发生变化,撤销也不会发生变化,点击清空按钮则全部清空。

主要思路:

1.文本框中的值用(String)session.getAttribute()来填充,第一次打开界面,用if语句来判断,显示空,跳转过来的就显示第一次输入的值。

2.ReceiveStartServlet.java,RevokeServlet.java,ClearServlet.java都跳转到起始界面。

 

3.文件读写,用到I/O流和file的知识,里面主要用到递归函数。

操作步骤:

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

输入目标路径,

输入你想要提取的文件类型,也就是后缀名,或者特定的后缀,

点击确定,目标目录下将生成你所需要的文件,

点击撤回就会清空目标目录下生成的文件,

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

程序介绍:

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

  start.jsp(显示界面)  

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

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

  RevokeServlet.java(撤回操作)

  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);        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">            <br>输入源路径:<input type="text" name="startPath"                value="<%if (session.getAttribute("startPath") == null) {                out.print("");            } else {                out.print(session.getAttribute("startPath"));            }%>" />            <br> 输目标路径:<input type="text" name="targetPath"                value="<%if (session.getAttribute("targetPath") == null) {                out.print("");            } else {                out.print(session.getAttribute("targetPath"));            }%>" />            <br> 输入后缀名:<input type="text" name="fileType"                value="<%if (session.getAttribute("fileType") == null) {                out.print("");            } else {                out.print(session.getAttribute("fileType"));            }%>" />            <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.servlet;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class ReceiveStartServlet extends HttpServlet {    private static final long serialVersionUID = 1L;    static int count = 1;    protected void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        doPost(request, response);    }    protected void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        String startPath = request.getParameter("startPath");        String targetPath = request.getParameter("targetPath");        String fileType = request.getParameter("fileType");        request.getSession().setAttribute("startPath", startPath);        request.getSession().setAttribute("targetPath", targetPath);        request.getSession().setAttribute("fileType", fileType);        try {            if (startPath != null && targetPath != null && fileType != null) {                cycle(startPath, targetPath, fileType, response);                try {                    response.sendRedirect("start/start.jsp");                } catch (IOException e) {                    e.printStackTrace();                }            }        } catch (Exception e) {            e.printStackTrace();        }    }    public static void cycle(String source_path, String target_path, String file_type, HttpServletResponse response) {        File file = new File(source_path);        String names[] = file.list();        // 判断是不是文件以及是否以你想要的文件类型结尾        if (file.isFile() && file.getAbsolutePath().endsWith(file_type)) {            String new_path = target_path + "/" + file.getName();            File file1 = new File(new_path);            if (!file1.exists()) {                try {                    file1.createNewFile();                } catch (IOException e) {                }            } else {                // 如果文件名字相同,在点前面加数字进行区分                // 注意用\\.进行分隔,而不是.                String[] arr = new_path.split("\\.");                String new_path1 = arr[0] + count + "." + arr[1];                file1.renameTo(new File(new_path1));            }            // 是文件,所以开始复制文件            fileCopyByBufferStreamArray(file.getAbsolutePath(), new_path, response);        }        else if (file.isFile() && !file.getAbsolutePath().endsWith(file_type)) {            // 注意这个方法体中什么都不写,就是不做处理        } else {            for (int i = 0; i < names.length; i++) {                // 不是文件,进行迭代                cycle(file.getAbsolutePath() + "/" + names[i], target_path, file_type, response);            }        }    }    public static void fileCopyByBufferStreamArray(String srcFile, String targetFile, HttpServletResponse response) {        // 用流的知识进行写文件        File file = new File(srcFile);        File file1 = new File(targetFile);        FileInputStream fis = null;        FileOutputStream fos = null;        BufferedInputStream bis = null;        BufferedOutputStream bos = null;        try {            fis = new FileInputStream(file);            fos = new FileOutputStream(file1);            bis = new BufferedInputStream(fis);            bos = new BufferedOutputStream(fos);            int len = 0;            byte[] b = new byte[10];            while ((len = bis.read(b)) != -1) {                bos.write(b, 0, len);            }            bos.flush();        } catch (IOException e) {        } finally {            try {                if (fis != null) {                    fis.close();                }                if (fos != null) {                    fos.close();                }                if (bis != null) {                    bis.close();                }                if (bos != null) {                    bos.close();                }            } catch (IOException e) {            }        }    }}

 

RevokeServlet.java

package com.hputt.servlet;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;public class RevokeServlet extends HttpServlet {    private static final long serialVersionUID = 1L;    protected void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        doPost(request, response);    }    protected void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        String startPath = (String) request.getSession().getAttribute("startPath");        if ("".equals(startPath)) {            File file = new File(startPath);            deleteFile(file, response);        }    }    private static void deleteFile(File file, HttpServletResponse response) {        // 记住不要把路径的那个文件夹删掉了        if (file.exists()) {            if (file.isFile()) {                // 是文件,直接删除                file.delete();            } else if (file.isDirectory()) {                File[] files = file.listFiles();                for (int i = 0; i < files.length; i++) {                    // 如果不是文件,进行迭代                    deleteFile(files[i], response);                }            }        }        try {            response.sendRedirect("start/start.jsp");        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}

 

ClearServlet.java

package com.hputt.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class ClearServlet extends HttpServlet {    private static final long serialVersionUID = 1L;    protected void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        doPost(request, response);    }    protected void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        request.getSession().removeAttribute("startPath");        request.getSession().removeAttribute("targetPath");        request.getSession().removeAttribute("fileType");        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;public class CodeFilter implements Filter {    public void destroy() {        // TODO Auto-generated method stub    }    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)            throws IOException, ServletException {        HttpServletRequest req = (HttpServletRequest) request;        if ("post".equalsIgnoreCase(req.getMethod())) {            req.setCharacterEncoding("utf-8");        }        chain.doFilter(request, response);    }    public void init(FilterConfig fConfig) throws ServletException {    }}

 

网页版批量提取目录下特定文件类型