• <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>

            Khan's Notebook GCC/GNU/Linux Delphi/Window Java/Anywhere

            路漫漫,長修遠,我們不能沒有錢
            隨筆 - 173, 文章 - 0, 評論 - 257, 引用 - 0
            數據加載中……

            java 上傳文件代碼,支持中文文件名和中文文件內容,可以同時提交多個參數

            提交的表單代碼:

            < table  width ="100%"  border ="0"  cellspacing ="1"  cellpadding ="0"  bgcolor ="#FF9900"  align ="center" >
              
            < form  ID =mainform   enctype ="multipart/form-data"  method ="post"  action ="file_Add.jsp?upload=true&parent_id=10" >
              
            < tr  bgcolor ="#FFFFFF" >
                
            < td  width ="15%"  height ="12" >< div  align ="center" ></ div ></ td >
                
            < td  width ="15%"  height ="12" >
                  
            < div  align ="left" ></ div >
                
            </ td >
                
            < td  width ="15%"  height ="12" >   < div  align ="center" > 主編號: </ div ></ td >
                
            < td  width ="15%"  height ="12" >
                  
            < div  align ="left" > 10 </ div >
                
            </ td >
              
            </ tr >

              
            < tr  bgcolor ="#FFFFFF" >
                
            < td  width ="15%"  height ="12" >< div  align ="center" > 文件路徑: </ div ></ td >
                
            < td  width ="15%"  height ="12" >
                  
            < div  align ="left" >< input  name ="filepath"  type ="text"  class ="smallInput"  id ="filepath"  size ="15"  value ="" ></ div >
                
            </ td >
                
            < td  width ="15%"  height ="12" >   < div  align ="center" > 文件類型: </ div ></ td >
                
            < td  width ="15%"  height ="12" >
                  
            < div  align ="left" >
                    
            < select  name ="file_type" >
                      
            < option  selected > mid </ option >
                      
            < option > mmf </ option >
                      
            < option > arm </ option >
                      
            < option > midi </ option >
                      
            < option > mp3 </ option >
                      
            < option > 3gp </ option >
                      
            < option > gif </ option >
                      
            < option > jpg </ option >
                      
            < option > psd </ option >
                    
            </ select >
                  
            </ div >
                
            </ td >
              
            </ tr >

              
            < tr  bgcolor ="#FFFFFF" >
                
            < td  width ="15%"  height ="12" >< div  align ="center" > 和弦數: </ div ></ td >
                
            < td  width ="15%"  height ="12" >
                  
            < div  align ="left" >
                    
            < select  name ="songstyle" >
                      
            < option  selected > 4 </ option >
                      
            < option > 16 </ option >
                      
            < option > 32 </ option >
                      
            < option > 40 </ option >
                      
            < option > 48 </ option >
                      
            < option > 60 </ option >
                    
            </ select >
                  
            </ div >
                
            </ td >
                
            < td  width ="15%"  height ="12" >   < div  align ="center" > 文件大小: </ div ></ td >
                
            < td  width ="15%"  height ="12" >
                  
            < div  align ="left" >< input  name ="filesize"  type ="text"  class ="smallInput"  readonly ="readonly"  id ="filesize"  size ="15"  value ="27" ></ div >
                
            </ td >
              
            </ tr >
              
            < tr  bgcolor ="#FFFFFF" >
                
            < td  width ="15%"  height ="12" > 上傳文件 </ td >
                
            < td  width ="15%"  height ="12" >< div  align ="left" >< input  name ="fileupload"  type ="file"  class ="smallInput"  id ="fileupload"  size ="15" ></ div ></ td >
                
            < td  width ="15%"  height ="12" >
                  
            < input  type ="submit"  name ="Submit"  value ="確定"  class ="smallInput" >
                
            </ td >
                
            < td  width ="15%"  height ="12" > &nbsp; </ td >
              
            </ tr >
            </ form >






            文件上傳的類:暫時只支持單個文件上傳

            package web.QiXiangTong;


            import javax.servlet.http.HttpServletRequest;
            import java.io.*;
            import java.util.HashMap;
            //import com.khan.net.*;


            /*
              version 1.0
              修正了傳遞二進制文件不正常的問題, 因為我將流轉成了字符串的方式解析, 導致二進制文件的不可見字符轉碼失敗
              解決, 全部采用byte解析
              暫時還是只能上傳單個文件
              支持傳遞文件的同時, 參數url參數
            */
            public class uploadFile  {
                public static final int MAX_SIZE = 1024 * 1024*100;
                public static final String FILE_DIR = "d:/temp/";

                private int file_Size=0;
                private String file_Path = "";
                private HashMap hm = new HashMap();

                public String upLoad(HttpServletRequest req) {
                    String tmpString ="";
                    String result = "";
                    DataInputStream dis = null;

                    try {
                        dis = new DataInputStream(req.getInputStream());
                        String content = req.getContentType();
                        if (content != null && content.indexOf("multipart/form-data") != -1) {

                            int reqSize = req.getContentLength();
                            byte[] data = new byte[reqSize];
                           
                            int bytesRead = 0;
                            int totalBytesRead = 0;
                            int sizeCheck = 0;
                            while (totalBytesRead < reqSize) {
                                // check for maximum file size violation
                                sizeCheck = totalBytesRead + dis.available();
                                if (sizeCheck > MAX_SIZE)
                                    result = "文件太大不能上傳...";

                                bytesRead = dis.read(data, totalBytesRead, reqSize);
                                totalBytesRead += bytesRead;
                            }

                            tmpString = new String(data);
                            hm = parseAnotherParam(tmpString);
            //System.out.println("src datatmp |"+new String(data)+"|");
                            int postion = arrayIndexOf(data, "\r\n".getBytes());
                            byte[] split_arr = new byte[postion];
                            System.arraycopy(data, 0, split_arr, 0, postion);
            //System.out.println("split |"+new String(split_arr)+"|");

                            postion = arrayIndexOf(data, "filename=\"".getBytes());
                            byte[] dataTmp = new byte[data.length - postion];
                            System.arraycopy(data, postion, dataTmp, 0, dataTmp.length);
                            data = null;
                            data = dataTmp.clone();


                            String filePath =null;
                            postion = arrayIndexOf(data, "Content-Type:".getBytes())-2;
                            dataTmp = null;
                            dataTmp = new byte[postion];
                            System.arraycopy(data, 0, dataTmp, 0, dataTmp.length);
                            filePath = new String(dataTmp);
                            if (filePath==null && filePath.equals("")) return "";
            //System.out.println("filename |"+filePath+"|");
                           // 分離contentType 并賦值
                            postion = arrayIndexOf(data, "Content-Type:".getBytes());
                            dataTmp = null;
                            dataTmp = new byte[data.length - postion];
                            System.arraycopy(data, postion, dataTmp, 0, dataTmp.length);
                            data = null;
                            data = dataTmp.clone();
            //System.out.println("src adatatmp |"+new String(data)+"|");
             
                            postion = arrayIndexOf(data, "\n".getBytes()) + 1;
                            dataTmp = null;
                            dataTmp = new byte[data.length - postion];
                            System.arraycopy(data, postion, dataTmp, 0, dataTmp.length);
                            data = null;
                            data = dataTmp.clone();
            //System.out.println("datatmp |"+new String(data)+"|");

                            // 分離文件信息 獲得最終想要的字節
                            postion = arrayIndexOf(data, split_arr);
                            split_arr = null;
                            dataTmp = null;
                            dataTmp = new byte[postion - 2];
                            System.arraycopy(data, 2, dataTmp, 0, dataTmp.length);
                            data = null;
                            data = dataTmp.clone();
            //System.out.println("datatmp |"+new String(data)+"|");

                            postion = arrayLastIndexOf(data, "\n".getBytes())-1;
                            dataTmp = null;
                            dataTmp = new byte[postion];
            //System.out.println("postion:"+postion + " datalength:"+ data.length +" tmplength:" + dataTmp.length);
                            System.arraycopy(data, 0, dataTmp, 0, dataTmp.length);

                            data = null;
            //System.out.println("data |"+new String(dataTmp)+"|");
                            String file_path = getFileName(filePath);
            //System.out.println("file_path:"+file_path);
                            if(null != file_path) {
                              if (writeFile(dataTmp, FILE_DIR + file_path)) {
                                this.file_Size = dataTmp.length;
                                this.file_Path = FILE_DIR + file_path;
                                result = "文件上傳完畢";
                              } else {
                                result = "文件上傳失敗";
                              }
                            }else{
                                result = "文件名為空";
                            }
                            dataTmp = null;
                        } else {
                            result = "content 必須為 multipart/form-data";
                        }
                    } catch (UnsupportedEncodingException ex4) {
                        result = "UnsupportedEncodingException錯誤";
                    } catch (NullPointerException e) {
                        result = "NullPointerException錯誤";
                    } catch (IOException ex1) {
                        result = "IOException 錯誤 ";
                    }catch (Exception ex1) {
                        result = "Exception 錯誤 ";
                    }

                    return result;
                }

                public String getFilePath(){
                    return this.file_Path;
                }

                public int getFileSize(){
                    return this.file_Size;
                }

                public boolean writeFile(byte[] data, String path) {
                    File f = null;
                    FileOutputStream fos = null;
                    try {
                        f = new File(path);
                        f.createNewFile();
                        fos = new FileOutputStream(f);
                        fos.write(data, 0, data.length);
                    } catch (FileNotFoundException e) {
                        return false;
                    } catch (IOException e) {
                        return false;
                    } finally {
                        try {
                            fos.close();
                        } catch (IOException e) {
                            return false;
                        }
                    }
                    return true;
                }

                public String getFileName(String arg) {
                    String path = "";
                    if(arg.equals("\"\"")) {
                        return null;
                    }

                    if (arg.indexOf("\"") > -1)
                        path = arg.substring(arg.indexOf("\"") + 1, arg.lastIndexOf("\""));
                    else
                        path = arg;
                //System.out.println("file_path:"+arg);
                    path = path.substring(path.lastIndexOf("\\") + 1);
                    return path;
                }


                //判斷兩個byte數組的值是否相等
                private boolean arrayEquals(byte[] src, byte[] value){
                    if(src == null || value == null)
                        return false;
                    if(src.length != value.length)
                        return false;

                    for(int i=0; i<src.length; i++) {
                        if(src[i] != value[i])
                            return false;
                    }
                    return true;
                }


                //找出value數組在src中的位置, 從前往后
                private int arrayIndexOf(byte[] src, byte[] value){
                    if(src == null || value == null)
                        return -1;
                    if(src.length < value.length)
                        return -1;

                    int postion = -1;

                    for(int i=0; i<src.length - value.length; i++) {
                        postion = i;
                        byte[] tmp = new byte[value.length];
                        System.arraycopy(src, i, tmp, 0, tmp.length);
                        if(arrayEquals(tmp, value)) {
                            tmp = null;
                            return postion;
                        }else{
                            postion = -1;
                            tmp = null;
                        }
                    }

                    return postion;
                }


                //找出value數組在src中的位置
                private int arrayLastIndexOf(byte[] src, byte[] value){
                    if(src == null || value == null)
                        return -1;
                    if(src.length < value.length)
                        return -1;

                    int postion = -1;

                    for(int i=src.length - value.length ; i >-1; i--) {
                        postion = i;

                        byte[] tmp = new byte[value.length];
                        System.arraycopy(src, i, tmp, 0, tmp.length);
            //System.out.println(i);
            //Common.PrintDataHex(tmp, " ");
            //Common.PrintDataHex(value, " ");

                        if(arrayEquals(tmp, value)) {
                            tmp = null;
                            return postion;
                        }else{
                            postion = -1;
                            tmp = null;
                        }
                    }
                    //System.out.println("debug");
                    return postion;
                }
                

                public HashMap parseAnotherParam(String str){
                  HashMap<String, String> hm= new HashMap<String, String>();
                  String key="";
                  String value="";
                  int startindex = 0;
                  int endindex = 0;

                  startindex = str.indexOf("Content-Disposition: form-data; name=\"")
                             + "Content-Disposition: form-data; name=\"".length();
                  endindex = str.indexOf("\"\r\n\r\n");

                  while ( startindex >-1 && endindex > -1 ){
                    key = str.substring(startindex, endindex);

                    if(!str.substring(endindex , endindex + 5).equals("\"\r\n\r\n")  ){//去掉沒有value的元素
                        str = str.substring(endindex);
                        startindex = str.indexOf("Content-Disposition: form-data; name=\"")
                                   + "Content-Disposition: form-data; name=\"".length();
                        endindex = str.indexOf("\"\r\n\r\n");
                        continue;
                    }
                    if( key.indexOf("\";") > -1){//去掉上傳文件的參數以及編碼
                       str = str.substring(str.indexOf("\";") + 2);
                       startindex = str.indexOf("Content-Disposition: form-data; name=\"")
                                  + "Content-Disposition: form-data; name=\"".length();
                       endindex = str.indexOf("\"\r\n\r\n");

                       continue;
                    } else
                        str = str.substring(endindex + 5);

                    value = str.substring(0, str.indexOf("\r\n"));
                    str = str.substring(str.indexOf("\r\n") + 2);
                    //System.out.println("key:"+key+" value:"+value);
                    hm.put(key,value);

                    startindex = str.indexOf("Content-Disposition: form-data; name=\"")
                               + "Content-Disposition: form-data; name=\"".length();
                    endindex = str.indexOf("\"\r\n\r\n");

                  }
                  return hm;
                }

                public String getParameter(String param){
                    //System.out.println(hm.toString());
                  return (String)hm.get(param);
                }




            }





            調用方式如下:
             
                com.khan.web.uploadFile  upload = new com.khan.web.uploadFile();

            //上傳文件
                out.print("<p><a>文件上傳狀態:" + upload.upLoad(request) + "</a></p>");
            //取得上傳文件的大小
                strs[0][5] = String.valueOf(upload.getFileSize()) ;

            //取得跟隨文件一起提交的表單中其他參數
                String filepath  = upload.getParameter("filepath");
                String file_type = upload.getParameter("file_type");
                String songstyle = upload.getParameter("songstyle");


            2006-8-17 15:38 修正文件上傳后會附加一段亂碼的bug
            計劃下一步增加同時上傳多個文件的功能

             


            posted on 2006-08-11 15:49 Khan 閱讀(11769) 評論(7)  編輯 收藏 引用 所屬分類: 跨平臺開發Java

            評論

            # re: java 上傳文件代碼,支持中文文件名和中文文件內容,可以同時提交多個參數  回復  更多評論   

            發現處理1m以上的文件上傳后不能使用,過段時間再改這個

            修正. 不是1m以上的文件上傳不正確. 而是二進制文件上傳后不能使用, 現已修正此問題...
            多文件同時上傳仍舊不支持.. 等我有空再說吧...
            2006-09-28 10:15 | Khan's Notebook

            # re: java 上傳文件代碼,支持中文文件名和中文文件內容,可以同時提交多個參數  回復  更多評論   

            請問一下,是否必須上傳文件之后才可以取得表單的其他參數呢?我用jspsamrtupload組件,必須要有文件上傳才可以取得表單里的其他參數,但是因為我上傳文件是有選擇性的,所以不需要每次都上傳,因此不能滿足需求,不知道你有沒有其他的方法可以滿足我的要求呢?能不能幫個忙?謝謝了
            2006-12-31 15:58 | Noriko

            # re: java 上傳文件代碼,支持中文文件名和中文文件內容,可以同時提交多個參數  回復  更多評論   

            如果沒有文件上傳..就按照普通的方式取得參數, request方式....
            這個部分多用嗅探器檢測一下http協議的數據包就知道了
            2007-01-05 12:38 | Khan's Notebook

            # re: java 上傳文件代碼,支持中文文件名和中文文件內容,可以同時提交多個參數  回復  更多評論   

            好是好.就是太長了
            2008-06-18 10:04 | 瘋子

            # re: java 上傳文件代碼,支持中文文件名和中文文件內容,可以同時提交多個參數  回復  更多評論   

            你如果看過jspsamrtupload的代碼. 就知道長是什么概念了...
            300行代碼搞定文件上傳. 我自己還是比較滿意的
            2008-06-18 13:07 | 思春貼調查員

            # re: java 上傳文件代碼,支持中文文件名和中文文件內容,可以同時提交多個參數  回復  更多評論   

            太長了 內容太繁瑣!
            2009-07-29 11:25 | 新手

            # re: java 上傳文件代碼,支持中文文件名和中文文件內容,可以同時提交多個參數  回復  更多評論   

            有沒有不用組件就可以直接上傳的java代碼?
            2012-05-10 17:40 | 瘋言瘋語
            人妻无码αv中文字幕久久| 97久久国产露脸精品国产| 国产精品久久久久无码av| 大蕉久久伊人中文字幕| 伊人久久大香线蕉综合热线| 奇米影视7777久久精品| 国产亚州精品女人久久久久久 | 久久久久国产日韩精品网站| 亚洲AV伊人久久青青草原| 日产精品久久久久久久性色 | 亚洲国产精品成人AV无码久久综合影院 | MM131亚洲国产美女久久| 久久本道久久综合伊人| 香蕉久久av一区二区三区| 国产成人无码精品久久久久免费| 7777精品伊人久久久大香线蕉| 91精品免费久久久久久久久| 久久精品国产AV一区二区三区| 国产一区二区精品久久凹凸| 99久久精品日本一区二区免费| 久久久这里有精品| 久久精品亚洲精品国产欧美| 久久精品人人做人人爽电影| 久久久久久精品无码人妻| 日本久久久久久久久久| 国产日韩久久久精品影院首页| 九九精品99久久久香蕉| 浪潮AV色综合久久天堂| 免费精品久久天干天干| 久久久午夜精品| 久久无码国产专区精品| 狠狠综合久久AV一区二区三区| 伊人久久大香线蕉无码麻豆| 久久精品国产亚洲精品| 久久99精品国产麻豆婷婷| 99热成人精品免费久久| 久久99精品国产麻豆蜜芽| 久久精品国产亚洲AV不卡| 亚洲国产小视频精品久久久三级| 精品久久久久久无码国产| 理论片午午伦夜理片久久 |