fileupload.java

来自「cwbbs 云网论坛源码」· Java 代码 · 共 622 行 · 第 1/2 页

JAVA
622
字号
package com.redmoon.kit.util;import java.io.*;import java.util.*;import javax.servlet.*;import javax.servlet.http.*;import cn.js.fan.web.*;import cn.js.fan.util.StrUtil;public class FileUpload {    boolean debug = false;    public String savePath;    String filepath, filename, contentType;    public Dictionary fields;    private int fileSize = 2048 * 1000;     String extname = "";     String[] extnames = null;    public Vector files = null;    public Vector tmpFiles = new Vector();     static Hashtable hash = new Hashtable();        static Random rand = new Random(System.currentTimeMillis());    static long lastRandTime = System.currentTimeMillis();    int ret = 1;    public static final int RET_SUCCESS = 1;    public static final int RET_FAIL = -1;    public static final int RET_TOOLARGEALL = -2;    public static final int RET_TOOLARGESINGLE = -3;    public static final int RET_INVALIDEXT = -4;    public HttpServletRequest request;    public FileUpload() {        files = new Vector();    }    protected void finalize() throws Throwable {        super.finalize();                Iterator ir = tmpFiles.iterator();        while (ir.hasNext()) {            String fpath = (String) ir.next();            File f = new File(fpath);            f.delete();        }    }        private int getDefaultFileSize() {        return fileSize;    }        public void setMaxFileSize(int size) {        fileSize = size;    }        public Vector getFiles() {        return files;    }        public int getRet() {        return ret;    }        public void setSavePath(String savePath) {        this.savePath = savePath;    }        public Enumeration getFields() {        return fields.keys();    }        public String getSavePath() {        return savePath;    }    public int getMaxAllFileSize() {        return maxAllFileSize;    }    public String getRealPath() {        return realPath;    }    public static String getTmpPath() {        return tmpPath;    }    public boolean isJspValid() {        return jspValid;    }    public boolean getHtmValid() {        return htmValid;    }        public String getFieldValue(String fieldName) {        if (fields == null || fieldName == null) {            return null;        }        String str = null;        try {            str = (String) fields.get(fieldName);        }        catch (ClassCastException e) {            System.out.println(getClass() + " field:" + fieldName + " class cast exception");            throw e;        }        return str;    }    public String[] getFieldValues(String fieldName) {        Object obj = fields.get(fieldName);        if (obj==null)            return null;        if (obj.getClass().isInstance(fieldName)) {            String[] r = new String[1];            r[0] = (String)obj;            return r;        }        else {            Vector v = (Vector) obj;            int len = v.size();            String[] r = new String[len];            for (int i=0; i<len; i++) {                r[i] = (String)v.get(i);            }            return r;        }    }        private void parseFileName(String s) {        if (s == null) {            return;        }        int pos = s.indexOf("filename=\"");        if (pos != -1) {            filepath = s.substring(pos + 10, s.length() - 1);            if (filepath.equals("")) {                return;             }            filename = getUploadFileName(filepath);            extname = getFileExt(filename);        }    }        private void setContentType(String s) {        if (s == null) {            return;        }        int pos = s.indexOf(": ");        if (pos != -1) {            contentType = s.substring(pos + 2, s.length());        }    }    public void setMaxAllFileSize(int maxAllFileSize) {        this.maxAllFileSize = maxAllFileSize;    }    public void setRealPath(String realPath) {        this.realPath = realPath;    }    public void setTmpPath(String tmpPath) {        this.tmpPath = tmpPath;    }    public void setJspValid(boolean jspValid) {        this.jspValid = jspValid;    }    public void setHtmValid(boolean htmValid) {        this.htmValid = htmValid;    }        public void setValidExtname(String[] extnames1) {        if (extnames1 == null)            return;        int len = extnames1.length;        extnames = new String[len];        for (int i = 0; i < len; i++) {            extnames[i] = extnames1[i];        }    }        private boolean isValidExtname(String ext) {        if (extnames == null) {            if (ext.equalsIgnoreCase("jsp")) {                if (jspValid)                     return true;                else                    return false;            }            else if (ext.equalsIgnoreCase("htm") || ext.equalsIgnoreCase("html")){                if (htmValid)                    return true;                else                    return false;            }            else                return true;        }        int len = extnames.length;        for (int i = 0; i < len; i++) {            if (extnames[i].equalsIgnoreCase(ext)) {                if (ext.equalsIgnoreCase("jsp")) {                    if (jspValid)                         return true;                    else                        return false;                }                else                    return true;            }        }        return false;    }        public String getErrMessage() {        String msg = "";        switch (ret) {        case RET_FAIL:            msg = "Upload failed.";             break;        case RET_TOOLARGEALL:            msg = "The total size of files exceed predefined max size " + getMaxAllFileSize() + ".";             break;        case RET_TOOLARGESINGLE:            msg = "Too large file. Every file should be limited in " + getDefaultFileSize() + " K";             break;        case RET_INVALIDEXT:            msg = "Invalid ext name.";             break;        case RET_SUCCESS:            msg = "Success.";             break;        default:            msg = "Error RET value.";         }        return msg;    }    public String getErrMessage(HttpServletRequest request) {        String msg = "";        switch (ret) {        case RET_FAIL:            msg = SkinUtil.LoadString(request, "RET_FAIL");             break;        case RET_TOOLARGEALL:            msg = SkinUtil.LoadString(request, "RET_TOOLARGEALL") + getMaxAllFileSize() + "K";             break;        case RET_TOOLARGESINGLE:            msg = SkinUtil.LoadString(request, "RET_TOOLARGESINGLE") + getDefaultFileSize() + " K";             break;        case RET_INVALIDEXT:            String str = "";            if (extnames!=null) {                int len = extnames.length;                for (int i=0; i<len; i++) {                    if (str.equals(""))                        str = extnames[i];                    else                        str += "," + extnames[i];                }            }            msg = StrUtil.format(SkinUtil.LoadString(request, "RET_INVALIDEXT"), new Object[] {str});             break;        case RET_SUCCESS:            msg = SkinUtil.LoadString(request, "RET_SUCCESS");             break;        default:            msg = SkinUtil.LoadString(request, "RET_ERROR");         }        return msg;    }    public int doUpload(ServletContext application, HttpServletRequest request) throws            IOException {        return doUpload(application, request, "utf-8");    }        public int doUpload(ServletContext application, HttpServletRequest request, String charset) throws            IOException {        this.request = request;        realPath = application.getRealPath("/");        if (realPath.lastIndexOf("/")!=realPath.length()-1)

⌨️ 快捷键说明

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