• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            xiaoguozi's Blog
            Pay it forword - 我并不覺的自豪,我所嘗試的事情都失敗了······習慣原本生活的人不容易改變,就算現狀很糟,他們也很難改變,在過程中,他們還是放棄了······他們一放棄,大家就都是輸家······讓愛傳出去,很困難,也無法預料,人們需要更細心的觀察別人,要隨時注意才能保護別人,因為他們未必知道自己要什么·····
            今天新寫了一個java文件上傳類,文件類型未限定,大小也未限定,可自己擴展,記下筆記
            /*
             * Function:    FileUpload
             * Writer:        JingJiaGuo
             * Date:        09.5.12
             * Mail:        guojingjia@stu.hdu.edu.cn
             
            */

            package JingJiaGuo;

            import javax.servlet.http.*;
            import java.io.*;
            import java.util.*;


            public class FileUpload {
                
            public class FileHoder{
                    
            public String name,filename,contentType;
                    
            public int start,end,length;
                }

                
            public class ValueHoder{
                    
            public String name,value;
                }

                
                
            public ArrayList<FileHoder> arrfile;
                
            public ArrayList<ValueHoder> arrvalue;
                
                
            public String FileNameSuffix(int index){
                    
            if(index<arrfile.size()){
                        
            int pos=arrfile.get(index).filename.lastIndexOf(".");
                        
            return arrfile.get(index).filename.substring(pos);
                    }
            else return null;
                }

                
            public String getFileName(int index){
                    
            if(index<arrfile.size()){
                        
            return arrfile.get(index).filename;
                    }
            else return null;
                }

                
            public int getFileSize(int index){
                    
            if(index<arrfile.size()){
                        
            return arrfile.get(index).length;
                    }
            else return 0;
                }

                
            public String getParamentName(int index){
                    
            if(index<arrvalue.size()){
                        
            return arrvalue.get(index).name;
                    }
            else return null;
                }

                
            public String getParamentValue(int index){
                    
            if(index<arrvalue.size()){
                        
            return arrvalue.get(index).value;
                    }
            else return null;
                }

                
            public void Clear(){
                    arrfile.clear();
                    arrvalue.clear();
                }

                
            public boolean Parse(HttpServletRequest request){
                    arrfile
            =new ArrayList<FileHoder>();
                    arrvalue
            =new ArrayList<ValueHoder>();
                    
            try{
                        String contenttype
            =request.getContentType();
                        String boundary
            ="boundary=";
                        
            int pos=contenttype.indexOf(boundary);
                        boundary
            =contenttype.substring(pos+boundary.length());
                        boundary
            ="--"+boundary;
                        
                        InputStream is
            =request.getInputStream();
                        
            int readlen=0,len=request.getContentLength();
                        buffer
            =new byte[len];
                        
            int read;
                        
            while((read=is.read(buffer,readlen,len-readlen))>0){
                            readlen
            +=read;
                        }

                        
            if(readlen<len){
                            
            return false;
                        }

                         
            int st=byteIndexOf(buffer,boundary,0,len);
                        
            int ed=0;
                        
            while(st>=0){
                            st
            +=boundary.length();// /r/n
                            ed=byteIndexOf(buffer,boundary,st,len);
                            
            if(ed!=-1){                    
                                Process(buffer,st
            +2,ed-2);
                            }

                            
            else break;
                            st
            =ed;
                        }

                    }
            catch(IOException e){
                        e.printStackTrace();
                        
            return false;
                    }

                    
            return true;
                }

                
            private void Process(byte[] buffer,int st,int ed)
                
            throws UnsupportedEncodingException{
                    
            int[] pos=new int[tokens.length];
                    
            for(int i=0;i<tokens.length;++i){
                        pos[i]
            =byteIndexOf(buffer,tokens[i],st,ed);
                    }

                    
            if(pos[1]==-1){
                        ValueHoder vh
            =new ValueHoder();
                        vh.name
            =subBytesString(buffer,pos[0]+ByteLength(tokens[0]),pos[2]);
                        vh.value
            =subBytesString(buffer,pos[4]+ByteLength(tokens[4]),ed);
                        arrvalue.add(vh);
                    }
            else{
                        FileHoder fh
            =new FileHoder();
                        fh.name
            =subBytesString(buffer,pos[0]+ByteLength(tokens[0]),pos[1]);
                        fh.filename
            =subBytesString(buffer,pos[1]+ByteLength(tokens[1]),pos[2]);
                        fh.contentType
            =subBytesString(buffer,pos[3]+ByteLength(tokens[3]),pos[4]);
                        fh.start
            =pos[4]+ByteLength(tokens[4]);
                        fh.end
            =ed;
                        fh.length
            =fh.end-fh.start;
                    
            //    fh.filecontent=subBytesString(buffer,pos[4]+ByteLength(tokens[4]),ed);
                        if(fh.length>0)arrfile.add(fh);
                    }

                }

                
            public void SaveTo(int index,String path)
                
            throws IOException,FileNotFoundException{
                    FileOutputStream fos
            =new FileOutputStream(path);
                    fos.write(buffer,arrfile.get(index).start,arrfile.get(index).length);
                    fos.close();
                }

                
            private  int ByteLength(String str) throws UnsupportedEncodingException{
                    
            byte[] tmp=str.getBytes();
                    
            return tmp.length;
                }

                
            private  String subBytesString(byte[] buffer,int from,int end,String encoding)
                
            throws UnsupportedEncodingException{
                    
            byte[] tmp=new byte[end-from];
                    
            for(int i=from;i<end;i++){
                        tmp[i
            -from]=buffer[i];
                    }

                    
            return new String(tmp,encoding);
                }

                
            private  String subBytesString(byte[] buffer,int from,int end)
                
            throws UnsupportedEncodingException{
                    
            return subBytesString(buffer,from,end,"UTF-8");        
                }

                
            private  int byteIndexOf(byte[] buffer,String src,int begin,int end){
                    
            byte[] boundary=src.getBytes();
                    
            for(int i=begin;i<end;i++){
                        
            boolean find=true;
                        
            for(int j=0;j<src.length()&&j+i<end;j++){
                            
            if(buffer[i+j]!=boundary[j]){
                                find
            =false;
                                
            break;
                            }

                        }

                        
            if(find){
                            
            return i;
                        }

                    }

                    
            return -1;
                }

                
            private byte[] buffer;
                
            private String[] tokens={"name=\"",
                    "\"; filename=\"",
                    
            "\"\r\n",
                    "Content-Type: ",
                    
            "\r\n\r\n"
                    }
            ;
            }


            example:
            <%@ page language="java" contentType="text/html; charset=utf-8"
                pageEncoding
            ="utf-8"%>
                
            <%@page import="java.io.*" %>
                
            <%@page import="JingJiaGuo.*" %>
            <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
            <html>
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
            <title>Insert title here</title>
            </head>
            <body>
            <%
                FileUpload myfile
            =new FileUpload();
                myfile.Parse(request);
                
            for(int i=0;i<myfile.arrfile.size();i++){
                    myfile.SaveTo(i,
            "D:\\"+myfile.getFileName(i));
                    out.write(myfile.getFileName(i)
            +"<br>");
                    out.write(myfile.arrfile.get(i).contentType
            +"<br>");
                    out.write(myfile.FileNameSuffix(i));
                    
            //out.write(myfile.arrfile.get(i).filecontent.length()+"<br>");
                }

                myfile.Clear();
            %>
            </body>
            </html>
            posted on 2009-05-12 22:57 小果子 閱讀(232) 評論(0)  編輯 收藏 引用 所屬分類: 學習筆記
            亚洲精品高清一二区久久| 无遮挡粉嫩小泬久久久久久久| 亚洲欧美日韩中文久久| 久久免费99精品国产自在现线| 狠狠色丁香婷婷久久综合不卡 | 女人香蕉久久**毛片精品| 久久久女人与动物群交毛片| 久久精品国产清自在天天线| 97久久婷婷五月综合色d啪蜜芽| 精品伊人久久久| 久久久久av无码免费网| 成人午夜精品无码区久久| 香蕉久久av一区二区三区| 久久久精品人妻一区二区三区蜜桃 | 欧美色综合久久久久久| 亚洲七七久久精品中文国产| 日韩十八禁一区二区久久| 中文成人久久久久影院免费观看 | 国产精品久久久久久久app| 亚洲伊人久久成综合人影院| 国产精品久久久香蕉| 人妻少妇久久中文字幕一区二区| 久久精品国产亚洲77777| 国产国产成人久久精品| 亚洲日本va午夜中文字幕久久 | 久久亚洲国产成人精品无码区| 国产亚州精品女人久久久久久 | 一97日本道伊人久久综合影院| 久久人妻无码中文字幕| 国产精品久久久久久影院| 88久久精品无码一区二区毛片 | 久久久精品日本一区二区三区| 女同久久| 久久人人爽爽爽人久久久| 色综合久久综合网观看| 久久久国产99久久国产一| A狠狠久久蜜臀婷色中文网| 久久精品国产亚洲5555| 精品久久久久成人码免费动漫| 99久久中文字幕| 久久婷婷五月综合97色直播|