⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filemanager.jsp

📁 jsp文件管理系统
💻 JSP
📖 第 1 页 / 共 3 页
字号:
<%@ page import="java.util.*,java.net.*,java.text.*,java.util.zip.*,java.io.*"%>
<%@ page contentType="text/html;charset=gb2312"%> 
<%!
/*
**************************************************************************************
*JSP 文件管理器 v1.001                                                               *
*Copyright (C) 2003 by Bagheera                                                      *
*E-mail:bagheera@beareyes.com                                                        *
*QQ:179189585                                                                        *
*http://jmmm.com                                                                     *
*------------------------------------------------------------------------------------*
*警告:请不要随便修改以上版权信息!                                                    *
**************************************************************************************
*#######免费空间管理系统正在完善之中,请到这里测试并发表宝贵意见:                    *
**http://jmmm.com/web/index.jsp    测试帐号:test  密码:test                          *
**************************************************************************************
*/

//编辑器显示列数
private static final int EDITFIELD_COLS =100;
//编辑器显示行数
private static final int EDITFIELD_ROWS = 30;
//-----------------------------------------------------------------------------

//改变上传文件是的缓冲目录(一般不需要修改)
private static String tempdir = ".";

public class FileInfo{
    public String name            = null,
                   clientFileName  = null,
                   fileContentType = null;
    private byte[] fileContents    = null;
    public File  file              = null;
    public StringBuffer sb = new StringBuffer(100);
    public void setFileContents(byte[] aByteArray){
        fileContents = new byte[aByteArray.length];
        System.arraycopy(aByteArray, 0, fileContents, 0, aByteArray.length);
    }
}

public class HttpMultiPartParser{
    private final String lineSeparator = System.getProperty("line.separator", "\n");
    private final int ONE_MB=1024*1024*1;

    public Hashtable processData(ServletInputStream is, String boundary, String saveInDir)
                             throws IllegalArgumentException, IOException {
        if (is == null) throw new IllegalArgumentException("InputStream");
        if (boundary == null || boundary.trim().length() < 1)
            throw new IllegalArgumentException("boundary");
        boundary = "--" + boundary;
        StringTokenizer stLine = null, stFields = null;
        FileInfo fileInfo = null;
        Hashtable dataTable = new Hashtable(5);
        String line = null, field = null, paramName = null;
        boolean saveFiles=(saveInDir != null && saveInDir.trim().length() > 0),
                isFile = false;
        if (saveFiles){
            File f = new File(saveInDir);
            f.mkdirs();
        }
        line = getLine(is);
        if (line == null || !line.startsWith(boundary))
            throw new IOException("未发现;"
                                 +" boundary = " + boundary
                                 +", line = "    + line);
        while (line != null){
            if (line == null || !line.startsWith(boundary)) return dataTable;
            line = getLine(is);
            if (line == null) return dataTable;
            stLine = new StringTokenizer(line, ";\r\n");
            if (stLine.countTokens() < 2) throw new IllegalArgumentException("出现错误!");
            line = stLine.nextToken().toLowerCase();
            if (line.indexOf("form-data") < 0) throw new IllegalArgumentException("出现错误!");
            stFields = new StringTokenizer(stLine.nextToken(), "=\"");
            if (stFields.countTokens() < 2) throw new IllegalArgumentException("出现错误!");
            fileInfo = new FileInfo();
            stFields.nextToken();
            paramName = stFields.nextToken();
            isFile = false;
            if (stLine.hasMoreTokens()){
                field    = stLine.nextToken();
                stFields = new StringTokenizer(field, "=\"");
                if (stFields.countTokens() > 1){
                    if (stFields.nextToken().trim().equalsIgnoreCase("filename")){
                        fileInfo.name=paramName;
                        String value = stFields.nextToken();
                        if (value != null && value.trim().length() > 0){
                            fileInfo.clientFileName=value;
                            isFile = true;
                        }
                        else{
                            line = getLine(is); // 去掉"Content-Type:"行
                            line = getLine(is); // 去掉空白行
                            line = getLine(is); // 去掉空白行
                            line = getLine(is); // 定位
                            continue;
                        }
                    }
                }
                else if (field.toLowerCase().indexOf("filename") >= 0){
                            line = getLine(is); // 去掉"Content-Type:"行
                            line = getLine(is); // 去掉空白行
                            line = getLine(is); // 去掉空白行
                            line = getLine(is); // 定位
                    continue;
                }
            }
            boolean skipBlankLine = true;
            if (isFile){
                line = getLine(is);
                if (line == null) return dataTable;
                if (line.trim().length() < 1) skipBlankLine = false;
                else{
                    stLine = new StringTokenizer(line, ": ");
                    if (stLine.countTokens() < 2)
                        throw new IllegalArgumentException("出现错误!");
                    stLine.nextToken(); 
                    fileInfo.fileContentType=stLine.nextToken();
                }
            }
            if (skipBlankLine){
                line = getLine(is);
                if (line == null) return dataTable;
            }
            if (!isFile){
                line = getLine(is);
                if (line == null) return dataTable;
                dataTable.put(paramName, line);
                //判断是否为目录
                if (paramName.equals("dir")){
                  saveInDir = line;
                  System.out.println(line);
                }
                line = getLine(is);
                continue;
            }
            try{
                OutputStream os = null;
                String path     = null;
                if (saveFiles)
                    os = new FileOutputStream(path = getFileName(saveInDir,
                                               fileInfo.clientFileName));
                else os = new ByteArrayOutputStream(ONE_MB);
                boolean readingContent = true;
                byte previousLine[] = new byte[2 * ONE_MB];
                byte temp[] = null;
                byte currentLine[] = new byte[2 * ONE_MB];
                int read, read3;
                if ((read = is.readLine(previousLine, 0, previousLine.length)) == -1) {
                    line = null;
                    break;
                }
                    while (readingContent){
                        if ((read3 = is.readLine(currentLine, 0, currentLine.length)) == -1) {
                            line = null;
                            break;
                        }
                        if (compareBoundary(boundary, currentLine)){
                            os.write( previousLine, 0, read );
                            os.flush();
                            line = new String( currentLine, 0, read3 );
                            break;
                        }
                        else{
                            os.write( previousLine, 0, read );
                            os.flush();
                            temp = currentLine;
                            currentLine = previousLine;
                            previousLine = temp;
                            read = read3;
                        }
                    }
                os.close();
                temp = null;
                previousLine = null;
                currentLine = null;
                if (!saveFiles){
                  ByteArrayOutputStream baos = (ByteArrayOutputStream)os;
                  fileInfo.setFileContents(baos.toByteArray());
                }
                else{
                  fileInfo.file = new File(path);
                  os = null;
                }
                dataTable.put(paramName, fileInfo);
            }
            catch (IOException e) {
              throw e;
            }
        }
        return dataTable;
    }

    // 比较数据
    private boolean compareBoundary(String boundary, byte ba[]){
        byte b;
        if (boundary == null || ba == null) return false;
        for (int i=0; i < boundary.length(); i++)
           if ((byte)boundary.charAt(i) != ba[i]) return false;
        return true;
    }


    private synchronized String getLine(ServletInputStream sis) throws IOException{
        byte   b[]  = new byte[1024];
        int    read = sis.readLine(b, 0, b.length), index;
        String line = null;
        if (read != -1){
           line = new String(b, 0, read);
           if ((index = line.indexOf('\n')) >= 0) line   = line.substring(0, index-1);
        }
        b = null;
        return line;
    }

    public String getFileName(String dir, String fileName) throws IllegalArgumentException{
        String path = null;
        if (dir == null || fileName == null) throw new IllegalArgumentException("目录或者文件不存在!");
        int index = fileName.lastIndexOf('/');
        String name = null;
        if (index >= 0) name = fileName.substring(index + 1);
        else name = fileName;
        index = name.lastIndexOf('\\');
        if (index >= 0) fileName = name.substring(index + 1);
        path = dir + File.separator + fileName;
        if (File.separatorChar == '/') return path.replace('\\', File.separatorChar);
        else return path.replace('/',  File.separatorChar);
    }
  }

  /**
  * 下面这个类是为文件和目录排序
  * @author bagheera
  * @version 1.001
  */
  class FileComp implements Comparator{
    int mode=1;
    /**
    * @排序方法 1=文件名, 2=大小, 3=日期
    */
    FileComp (int mode){
      this.mode=mode;
    }
    public int compare(Object o1, Object o2){
      File f1 = (File)o1;
      File f2 = (File)o2;
      if (f1.isDirectory()){
        if (f2.isDirectory()){
          switch(mode){
            case 1:return f1.getAbsolutePath().toUpperCase().compareTo(f2.getAbsolutePath().toUpperCase());
            case 2:return new Long(f1.length()).compareTo(new Long(f2.length()));
            case 3:return new Long(f1.lastModified()).compareTo(new Long(f2.lastModified()));
            default:return 1;
          }
        }
        else return -1;
      }
      else if (f2.isDirectory()) return 1;
      else{
          switch(mode){
            case 1:return f1.getAbsolutePath().toUpperCase().compareTo(f2.getAbsolutePath().toUpperCase());
            case 2:return new Long(f1.length()).compareTo(new Long(f2.length()));
            case 3:return new Long(f1.lastModified()).compareTo(new Long(f2.lastModified()));
            default:return 1;
          }
      }
    }
  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -