JSP簡單文件管理

 相信下面幾個頁面很少有人愿意放到自己的網站上,寫出來僅供消遣了。

 

1.主頁面,List所有文件及文件夾

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="java.io.File"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Iterator"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
    String fileName = request.getParameter("fileName");
    if (fileName == null) {
        fileName = "/";
    }
    File file = new File(fileName);
    String par = file.getParent();

    out.println("<a href='?fileName=" + par + "'>Up ...</a><br><br>");

    out.println("Current Path: " + fileName + "<br><br>");
    Map<String, String> folders = getSubFolders(file);
    Iterator<String> it = folders.keySet().iterator();
    String key;
    while (it.hasNext()) {
        key = it.next();
        out.println("    <a href='?fileName="
                + folders.get(key) + "'>" + key + "</a><br>");
    }
    Map<String, String> files = getSubFileList(file);
    it = files.keySet().iterator();
    while (it.hasNext()) {
        key = it.next();
        if (editable(key)) {
            out
                    .println("    <a href='edit.jsp?fileName="
                            + files.get(key)
                            + "Action=Edit'><img src='images/edit.bmp' style='width: 15px; height: 15px;border: 0;'/></a><a href='printFile.jsp?fileName="
                            + files.get(key) + "'>" + key + "</a><br>");
        } else {
            out.println("    " + key + "<br>");
        }
    }
%>

<%!Map<String, String> getSubFileList(File file) {
        Map<String, String> result = new HashMap<String, String>();
        if (file.listFiles() != null) {
            for (File f : file.listFiles()) {
                if (f.isFile()) {
                    result.put(f.getName(), f.getAbsolutePath());
                }
            }
        }
        return result;
    }

    Map<String, String> getSubFolders(File file) {
        Map<String, String> result = new HashMap<String, String>();
        if (file.listFiles() != null) {
            for (File f : file.listFiles()) {
                if (f.isDirectory()) {
                    result.put(f.getName(), f.getAbsolutePath());
                }
            }
        }
        return result;
    }

    boolean editable(String name) {
        namename = name.toUpperCase();
        if (name.endsWith(".JAVA") || name.endsWith(".XML")
                || name.endsWith(".LOG") || name.endsWith(".JSP")
                || name.endsWith(".HTM") || name.endsWith(".TXT")
                || name.endsWith(".HTML") || name.endsWith(".SQL")
                || name.endsWith(".POLICY") || name.endsWith(".PROPERTIES")
                || name.indexOf(".") <= 0 || name.endsWith(".BAT")
                || name.endsWith(".SH") || name.endsWith(".CSS")
                || name.endsWith(".JS")) {
            return true;
        } else {
            return false;
        }
    }%>
</body>
</html>

 

2.打印文本文件頁面:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="java.io.FileInputStream"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<%
    String fileName = request.getParameter("fileName");
%>
<title><%=fileName%></title>
</head>
<body>
<a href="javascript:history.go(-1);">Back</a>
<br>
<br>
<pre>
<%
    try{
        FileInputStream input = new FileInputStream(fileName);
        byte[] buff = new byte[1024];
        int n = 0;
        while ((n = input.read(buff)) != -1) {
            out.println(toHtmlText(new String(buff, 0, n)));
        }
        input.close();
    }catch(Exception ex){
        
    }
%>
<%!String toHtmlText(String s) {
        if (s == null)
            return s;
        s = strReplace(s, "&", "&");
        s = strReplace(s, "<", "<");
        s = strReplace(s, ">", ">");
        s = strReplace(s, "/"", """);
        s = strReplace(s, " ", " ");
        return s;
    }

    String strReplace(String sBody, String sFrom, String sTo) {
        int i, j, k, l;
        i = 0;
        l = 0;
        j = sFrom.length();
        k = sTo.length();
        if (k < j)
            k = sBody.length();
        else
            k = sBody.length() * 2;
        StringBuffer ret = new StringBuffer(k);

        while (sBody.indexOf(sFrom, i) != -1) {
            ret.append(sBody.substring(i, sBody.indexOf(sFrom, i)) + sTo);
            i = sBody.indexOf(sFrom, i);
            i += j;
        }
        ret.append(sBody.substring(i));
        return ret.toString();
    }%>
</pre>
</body>
</html>

 

3.文本文件修改頁面

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%@page import="java.io.File"%>
<%@page import="java.io.OutputStream"%>
<%@page import="java.io.FileOutputStream"%>
<%@page import="java.io.BufferedWriter"%>
<%@page import="java.io.OutputStreamWriter"%>
<%@page import="java.io.FileInputStream"%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Editing</title>
</head>
<body>
<%
    String fileName = request.getParameter("fileName");
    String content = request.getParameter("content");
    String action = request.getParameter("Action");

    StringBuilder sb = new StringBuilder("");
    String title = "";
    
    try {
        if("Edit".equals(action)){
            FileInputStream input = new FileInputStream(fileName);
            byte[] buff = new byte[1024];
            int n = 0;
            while ((n = input.read(buff)) != -1) {
                sb.append(toHtmlText(new String(buff, 0, n)));
            }
            title = fileName;
            input.close();
        }else if(fileName != null) {
            System.out.println("Saving!");
            File file = new File(fileName);
            OutputStream stream = new FileOutputStream(file);
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(stream, "utf-8"));
            writer.write(content);
            writer.close();
            stream.close();
        }else{
            sb.append("");
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
%>
<form action="edit.jsp?Action=Save" method="POST">File Name<input type="text"
    id="fileName" name="fileName" value="<%=title %>"><br>
Content<textarea rows="35" cols="150" id="content" name="content"><%=sb.toString() %></textarea><br>
<input type="submit" value="Save"></form>
</body>
<%!String toHtmlText(String s) {
        if (s == null)
            return s;
        s = strReplace(s, "&", "&");
        s = strReplace(s, "<", "<");
        s = strReplace(s, ">", ">");
        s = strReplace(s, "/"", """);
        s = strReplace(s, " ", " ");
        return s;
    }

    String strReplace(String sBody, String sFrom, String sTo) {
        int i, j, k, l;
        i = 0;
        l = 0;
        j = sFrom.length();
        k = sTo.length();
        if (k < j)
            k = sBody.length();
        else
            k = sBody.length() * 2;
        StringBuffer ret = new StringBuffer(k);

        while (sBody.indexOf(sFrom, i) != -1) {
            ret.append(sBody.substring(i, sBody.indexOf(sFrom, i)) + sTo);
            i = sBody.indexOf(sFrom, i);
            i += j;
        }
        ret.append(sBody.substring(i));
        return ret.toString();
    }%>
</html>

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个简单的JSP员工管理系统,它实现了以下功能: 1. 员工信息的录入、修改、删除和查询; 2. 员工信息的分页显示; 3. 员工信息的导入和导出。 本系统使用了MySQL数据库,所以需要先创建一个名为"employee"的数据库,并在其中创建一个名为"employee_info"的表,表结构如下: CREATE TABLE `employee_info` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL, `gender` varchar(10) NOT NULL, `age` int(11) NOT NULL, `phone` varchar(20) NOT NULL, `email` varchar(50) NOT NULL, `address` varchar(100) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; 系统的主要文件包括: 1. index.jsp:系统首页,包括菜单栏和员工信息列表; 2. add.jsp:添加员工信息页面; 3. edit.jsp:修改员工信息页面; 4. delete.jsp:删除员工信息页面; 5. search.jsp:查询员工信息页面; 6. import.jsp:导入员工信息页面; 7. export.jsp:导出员工信息页面; 8. Employee.java:员工信息实体类; 9. EmployeeDao.java:员工信息数据库操作类; 10. EmployeeService.java:员工信息逻辑操作类; 11. DBUtil.java:数据库连接工具类。 系统的实现过程: 1. 首先在index.jsp中显示员工信息列表,包括员工的ID、姓名、性别、年龄、电话、邮箱和地址; 2. 在菜单栏中提供添加、修改、删除、查询、导入和导出六个功能按钮; 3. 点击添加按钮,跳转到add.jsp页面,要求填写员工信息,包括姓名、性别、年龄、电话、邮箱和地址; 4. 点击保存按钮,将员工信息保存到数据库中,并跳转回index.jsp页面; 5. 点击修改按钮,跳转到edit.jsp页面,显示当前员工的信息,并允许修改; 6. 点击保存按钮,将修改后的员工信息更新到数据库中,并跳转回index.jsp页面; 7. 点击删除按钮,跳转到delete.jsp页面,显示当前员工的信息,并允许删除; 8. 点击确认按钮,将当前员工信息从数据库中删除,并跳转回index.jsp页面; 9. 点击查询按钮,跳转到search.jsp页面,要求输入员工姓名或电话号码; 10. 点击查询按钮,从数据库中查询符合条件的员工信息,并在index.jsp页面中显示; 11. 点击导入按钮,跳转到import.jsp页面,允许上传Excel文件; 12. 点击导入按钮,将Excel文件中的员工信息导入到数据库中,并跳转回index.jsp页面; 13. 点击导出按钮,跳转到export.jsp页面,允许选择导出员工信息的条件(如姓名、电话等); 14. 点击导出按钮,将符合条件的员工信息导出到Excel文件中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值