JSP簡單文件管理

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

 

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

  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  2.     pageEncoding="ISO-8859-1"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <%@page import="java.io.File"%>
  5. <%@page import="java.util.Map"%>
  6. <%@page import="java.util.HashMap"%>
  7. <%@page import="java.util.Iterator"%>
  8. <html>
  9. <head>
  10. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  11. <title>Insert title here</title>
  12. </head>
  13. <body>
  14. <%
  15.     String fileName = request.getParameter("fileName");
  16.     if (fileName == null) {
  17.         fileName = "/";
  18.     }
  19.     File file = new File(fileName);
  20.     String par = file.getParent();
  21.     out.println("<a href='?fileName=" + par + "'>Up ...</a><br><br>");
  22.     out.println("Current Path: " + fileName + "<br><br>");
  23.     Map<String, String> folders = getSubFolders(file);
  24.     Iterator<String> it = folders.keySet().iterator();
  25.     String key;
  26.     while (it.hasNext()) {
  27.         key = it.next();
  28.         out.println("    <a href='?fileName="
  29.                 + folders.get(key) + "'>" + key + "</a><br>");
  30.     }
  31.     Map<String, String> files = getSubFileList(file);
  32.     it = files.keySet().iterator();
  33.     while (it.hasNext()) {
  34.         key = it.next();
  35.         if (editable(key)) {
  36.             out
  37.                     .println("    <a href='edit.jsp?fileName="
  38.                             + files.get(key)
  39.                             + "Action=Edit'><img src='images/edit.bmp' style='width: 15px; height: 15px;border: 0;'/></a><a href='printFile.jsp?fileName="
  40.                             + files.get(key) + "'>" + key + "</a><br>");
  41.         } else {
  42.             out.println("    " + key + "<br>");
  43.         }
  44.     }
  45. %>
  46. <%!Map<String, String> getSubFileList(File file) {
  47.         Map<String, String> result = new HashMap<String, String>();
  48.         if (file.listFiles() != null) {
  49.             for (File f : file.listFiles()) {
  50.                 if (f.isFile()) {
  51.                     result.put(f.getName(), f.getAbsolutePath());
  52.                 }
  53.             }
  54.         }
  55.         return result;
  56.     }
  57.     Map<String, String> getSubFolders(File file) {
  58.         Map<String, String> result = new HashMap<String, String>();
  59.         if (file.listFiles() != null) {
  60.             for (File f : file.listFiles()) {
  61.                 if (f.isDirectory()) {
  62.                     result.put(f.getName(), f.getAbsolutePath());
  63.                 }
  64.             }
  65.         }
  66.         return result;
  67.     }
  68.     boolean editable(String name) {
  69.         namename = name.toUpperCase();
  70.         if (name.endsWith(".JAVA") || name.endsWith(".XML")
  71.                 || name.endsWith(".LOG") || name.endsWith(".JSP")
  72.                 || name.endsWith(".HTM") || name.endsWith(".TXT")
  73.                 || name.endsWith(".HTML") || name.endsWith(".SQL")
  74.                 || name.endsWith(".POLICY") || name.endsWith(".PROPERTIES")
  75.                 || name.indexOf(".") <= 0 || name.endsWith(".BAT")
  76.                 || name.endsWith(".SH") || name.endsWith(".CSS")
  77.                 || name.endsWith(".JS")) {
  78.             return true;
  79.         } else {
  80.             return false;
  81.         }
  82.     }%>
  83. </body>
  84. </html>

2.打印文本文件頁面:

  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  2.     pageEncoding="ISO-8859-1"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <%@page import="java.io.FileInputStream"%>
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  8. <%
  9.     String fileName = request.getParameter("fileName");
  10. %>
  11. <title><%=fileName%></title>
  12. </head>
  13. <body>
  14. <a href="javascript:history.go(-1);">Back</a>
  15. <br>
  16. <br>
  17. <pre>
  18. <%
  19.     try{
  20.         FileInputStream input = new FileInputStream(fileName);
  21.         byte[] buff = new byte[1024];
  22.         int n = 0;
  23.         while ((n = input.read(buff)) != -1) {
  24.             out.println(toHtmlText(new String(buff, 0, n)));
  25.         }
  26.         input.close();
  27.     }catch(Exception ex){
  28.         
  29.     }
  30. %>
  31. <%!String toHtmlText(String s) {
  32.         if (s == null)
  33.             return s;
  34.         s = strReplace(s, "&", "&");
  35.         s = strReplace(s, "<", "<");
  36.         s = strReplace(s, ">", ">");
  37.         s = strReplace(s, "/"", """);
  38.         s = strReplace(s, " ", " ");
  39.         return s;
  40.     }
  41.     String strReplace(String sBody, String sFrom, String sTo) {
  42.         int i, j, k, l;
  43.         i = 0;
  44.         l = 0;
  45.         j = sFrom.length();
  46.         k = sTo.length();
  47.         if (k < j)
  48.             k = sBody.length();
  49.         else
  50.             k = sBody.length() * 2;
  51.         StringBuffer ret = new StringBuffer(k);
  52.         while (sBody.indexOf(sFrom, i) != -1) {
  53.             ret.append(sBody.substring(i, sBody.indexOf(sFrom, i)) + sTo);
  54.             i = sBody.indexOf(sFrom, i);
  55.             i += j;
  56.         }
  57.         ret.append(sBody.substring(i));
  58.         return ret.toString();
  59.     }%>
  60. </pre>
  61. </body>
  62. </html>

3.文本文件修改頁面

  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  2.     pageEncoding="ISO-8859-1"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <%@page import="java.io.File"%>
  6. <%@page import="java.io.OutputStream"%>
  7. <%@page import="java.io.FileOutputStream"%>
  8. <%@page import="java.io.BufferedWriter"%>
  9. <%@page import="java.io.OutputStreamWriter"%>
  10. <%@page import="java.io.FileInputStream"%>
  11. <head>
  12. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  13. <title>Editing</title>
  14. </head>
  15. <body>
  16. <%
  17.     String fileName = request.getParameter("fileName");
  18.     String content = request.getParameter("content");
  19.     String action = request.getParameter("Action");
  20.     StringBuilder sb = new StringBuilder("");
  21.     String title = "";
  22.     
  23.     try {
  24.         if("Edit".equals(action)){
  25.             FileInputStream input = new FileInputStream(fileName);
  26.             byte[] buff = new byte[1024];
  27.             int n = 0;
  28.             while ((n = input.read(buff)) != -1) {
  29.                 sb.append(toHtmlText(new String(buff, 0, n)));
  30.             }
  31.             title = fileName;
  32.             input.close();
  33.         }else if(fileName != null) {
  34.             System.out.println("Saving!");
  35.             File file = new File(fileName);
  36.             OutputStream stream = new FileOutputStream(file);
  37.             BufferedWriter writer = new BufferedWriter(
  38.                     new OutputStreamWriter(stream, "utf-8"));
  39.             writer.write(content);
  40.             writer.close();
  41.             stream.close();
  42.         }else{
  43.             sb.append("");
  44.         }
  45.     } catch (Exception ex) {
  46.         ex.printStackTrace();
  47.     }
  48. %>
  49. <form action="edit.jsp?Action=Save" method="POST">File Name<input type="text"
  50.     id="fileName" name="fileName" value="<%=title %>"><br>
  51. Content<textarea rows="35" cols="150" id="content" name="content"><%=sb.toString() %></textarea><br>
  52. <input type="submit" value="Save"></form>
  53. </body>
  54. <%!String toHtmlText(String s) {
  55.         if (s == null)
  56.             return s;
  57.         s = strReplace(s, "&", "&");
  58.         s = strReplace(s, "<", "<");
  59.         s = strReplace(s, ">", ">");
  60.         s = strReplace(s, "/"", """);
  61.         s = strReplace(s, " ", " ");
  62.         return s;
  63.     }
  64.     String strReplace(String sBody, String sFrom, String sTo) {
  65.         int i, j, k, l;
  66.         i = 0;
  67.         l = 0;
  68.         j = sFrom.length();
  69.         k = sTo.length();
  70.         if (k < j)
  71.             k = sBody.length();
  72.         else
  73.             k = sBody.length() * 2;
  74.         StringBuffer ret = new StringBuffer(k);
  75.         while (sBody.indexOf(sFrom, i) != -1) {
  76.             ret.append(sBody.substring(i, sBody.indexOf(sFrom, i)) + sTo);
  77.             i = sBody.indexOf(sFrom, i);
  78.             i += j;
  79.         }
  80.         ret.append(sBody.substring(i));
  81.         return ret.toString();
  82.     }%>
  83. </html>

 

 

转载于:https://www.cnblogs.com/moonsnow/archive/2008/08/04/6226887.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值