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

📄 formparser.java

📁 Farse uploading file
💻 JAVA
字号:
package include;import java.io.*;import java.util.ArrayList;import javax.servlet.ServletInputStream;/* * To change this template, choose Tools | Templates * and open the template in the editor. *//** * * @author James Bond 007 */public class FormParser {//Sub class    public class SFile{        public String filename="";        public long filesize=0;        public String tempname="";        public String uboxname = "";        public String fileType = "";        public SFile(){            filename="";            filesize=0;            tempname="";            uboxname = "";            fileType = "";        }        public boolean MoveFile(String NewDes){            NewDes = convertSystemPath(NewDes);            if (tempname.equals("")) return false;            try{                File tmp = new File(tempname);                System.out.println("Moving file '" + tempname + "' to '" + NewDes + "'");                if(!tmp.exists()) return false;                //System.out.print(NewDes);                return tmp.renameTo(new File(NewDes));            }catch(Exception ex){                System.out.println(ex.getMessage());            }            System.out.println("Moving file '" + tempname + "' to '" + NewDes + "'");            return false;        }        public String convertSystemPath(String path){        if(File.separator.equals("\\"))            return path.replace("/", File.separator);        else            return path.replace("\\", File.separator);        }        public String convertWebPath(String path){            return path.replace("\\", "/");        }        public String getFileName(){            return filename;        }        public long getFileSize(){            return filesize;        }        public String getTempName(){            return tempname;        }        public String getUploadBoxName(){            return uboxname;        }    }    public class MyParam{        public String name="";        public String value="";        public MyParam(String Name,String Value){            this.name=Name;            this.value=Value;        }    }//Variable    ServletInputStream in=null;    StringBuilder out = new StringBuilder("");    public String tmppath = "";    public ArrayList<SFile> FileList = new ArrayList<SFile>();    public ArrayList<MyParam> ParamList = new ArrayList<MyParam>();    public boolean isMultipartFormdata = false;//Function    public String getParameter(String name){        int n = ParamList.size();        for (int i=0;i<n;i++)            if(ParamList.get(i).name.equals(name))                return ParamList.get(i).value;        return null;    }    public String getParameter(String name, String strdefault){        String v = getParameter(name);        if(v==null)            return strdefault;        return v;    }    public String[] getParameterValue(String name){        int count=0;        int n = ParamList.size();        ArrayList<String> b = new ArrayList<String>();        for (int i=0;i<n;i++)            if(ParamList.get(i).name.equals(name)){                b.add(ParamList.get(i).value);            }        if(b.size()==0) return null;        String[] r = new String[b.size()];        for(int i=0;i<b.size();i++)            r[i]=b.get(i);        return r;    }    public void setTempPath(String tp){        tmppath=tp;    }    public String getTempPath(){        return tmppath;    }    public String getstr(String dis, String find, String end){        int pos = dis.lastIndexOf(find);        if(pos>=0){            String r = dis.substring(pos+find.length());            if(end.equals("")){                return r;            }else{                int pos2 = r.indexOf(end);                if(pos2>=0)                    return r.substring(0,pos2);                else                    return null;            }        }        return null;    }    public FormParser(ServletInputStream in) throws IOException{        String boundary = "";                                       //Holding the boundary        byte[] bLine = new byte[128];                               //Data in byte array        byte[] crlf = {(byte) 0x0D, (byte) 0x0A};                   // \r\n        String Line = "";                                           //Data in String        int i = in.readLine(bLine, 0, 128);                         //Read the first line        if(i!=-1) //Submit empty            Line = new String(bLine,0,i);                                   //Try to find out what the data is        if (Line.startsWith("-----")){            this.isMultipartFormdata = true;            boundary = Line.substring(0,Line.length()-2);                                       //For sure the boundary never > 128 byte            //Yeah things become difficult            while(i!=-1){ //While not end of stream                i = in.readLine(bLine, 0, 128);                if(i!=-1)                    Line = new String(bLine,0,i);                else                    break;                if(Line.startsWith("Content-Disposition: form-data;")){                    if(Line.contains("filename=\"")){ //Ha ha you got a file                        SFile sf = new SFile();                        sf.filename = this.getstr(Line, "filename=\"", "\"");                        sf.uboxname = this.getstr(Line, " name=\"", "\"");                        File f = File.createTempFile("JFParser", "tmp");                        FileOutputStream o = new FileOutputStream(f);                        i = in.readLine(bLine, 0, 128); //This must be a Content-Type line                        Line = new String(bLine,0,i);                        if(Line.startsWith("Content-Type: ")){ //Just check if everything ok                            sf.fileType = Line.substring(14,Line.length()-2);                        }                        else    //WTF. This must be Content-type                            {out.append("Error, Got a file but can't get type\n"); return;}                        i = in.readLine(bLine, 0, 128); //This must be an empty line                        Line = "";                        i = in.readLine(bLine, 0, 128);                        Line = new String(bLine,0,i);                        while(i!=-1 && !Line.startsWith(boundary)){                            o.write(bLine,0,i);                            i = in.readLine(bLine, 0, 128);                            if(i!=-1)                                Line = new String(bLine,0,i);                        }                        o.close();                        sf.tempname = f.getAbsolutePath();                        sf.filesize = f.length();                        if(f.length()>2)                            this.FileList.add(sf);                    }else{  //Just a normal data                        String name = this.getstr(Line, " name=\"", "\"");                        String value = "";                        i = in.readLine(bLine, 0, 128); //This must be an empty line                        Line = "";                        while(i!=-1 && !Line.startsWith(boundary)){                            value += Line;                            i = in.readLine(bLine, 0, 128);                            if(i!=-1){                                Line = new String(bLine,0,i);                            }                        }                        value = value.substring(0,value.length()-2);                        MyParam p = new MyParam(name, value);                        this.ParamList.add(p);                    }                }else{                    //WTF                    out.append("Error, Expected Content-Disposition \n"); return;                }            }        }else{            this.isMultipartFormdata = false;                       //Simple thing just try to read all and frase the normal post            String data = "";            while(i != -1){ //While read OK, not end of file                data += new String(bLine,0,i);                i = in.readLine(bLine, 0, 128);            }            //Okey we got the normal data: name1=adasd&name2=juifsi&name3=....            //System.out.println("FullData: " + data);            String[] datalist = data.split("&");            for(int d=0;d<datalist.length;d++){                String[] dt = datalist[d].split("=");                //System.out.println("Got " + datalist[d]);                if(dt.length>1){    //Ok safe to move                    //System.out.println("Name=" + dt[0] + " | Value=" + dt[1]);                    MyParam p = new MyParam(dt[0], dt[1]);                    this.ParamList.add(p);                }            }        }    }    @Override    protected void finalize() throws Throwable {        super.finalize();        for(int i=0;i< FileList.size();i++){            new File(FileList.get(i).tempname).delete();        }    }}

⌨️ 快捷键说明

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