欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

smartupload實(shí)現(xiàn)文件上傳時(shí)獲取表單數(shù)據(jù)(推薦)

 更新時(shí)間:2016年12月12日 16:05:02   作者:heartbeaty  
這篇文章主要介紹了smartupload實(shí)現(xiàn)文件上傳時(shí)獲取表單數(shù)據(jù)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下

實(shí)現(xiàn)文件上傳的form表單必須滿足兩個(gè)條件:method="post" enctype="multipart/form-data"

表單中enctype="multipart/form-data"的意思是設(shè)置表單的MIME編碼。默認(rèn)情況,這個(gè)編碼格式是application/x-www-form-urlencoded,不能用于文件上傳;只有使用了multipart/form-data,才能完整的傳遞文件數(shù)據(jù)。enctype="multipart/form-data"是上傳二進(jìn)制數(shù)據(jù); form里面的input的值以二進(jìn)制的方式傳過去。所以request就得不到值了, 也就是說加了這段代碼,用request就會傳遞不成功。

取表單字段值時(shí),用下面的方式:

SmartUpload su = new SmartUpload(); //新建一個(gè)SmartUpload對象 
su.getRequest().getParameterValues(String name); //取數(shù)組值  
su.getRequest().getParameter(String name); //取單個(gè)參數(shù)單個(gè)值 

注:在使用SmartUpload時(shí)需要添加相應(yīng)的jar包

String softname=su.getRequest().getParameter("softname");

注意:一定要在su.upload();,之后使用,才可以獲得值?。?/span>

<span style="font-size:14px;">public void doPost(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
      SmartUpload mySmartUpload = new SmartUpload(); 
      try{ 
        // Initialization 
        mySmartUpload.initialize(config,request,response); 
        mySmartUpload.setMaxFileSize(10*1024*1024);//限制上傳文件的大小 
        //mySmartUpload.setAllowedFilesList("txt,html,jpg,js");//設(shè)置允許上傳的文件類型 
        mySmartUpload.setDeniedFilesList("exe,doc");//設(shè)置禁止上傳的文件列表 
        mySmartUpload.upload(); 
        String hString = mySmartUpload.getRequest().getParameter("name1"); 
        //System.out.println(hString); 
        //System.out.println(mySmartUpload.getFiles().getCount()); 
        for(int i = 0 ;i <mySmartUpload.getFiles().getCount();i++){//多個(gè)文件的上傳 
          File file = mySmartUpload.getFiles().getFile(i); 
          if(file.getSize()!=0){ 
            //拼湊上傳文件的新名稱 
            String fileNameString = System.currentTimeMillis()+"."+file.getFileExt(); 
            //通過servlet的實(shí)際路徑拼湊上傳文件的保存路徑,實(shí)際使用需要修改此路徑 
            String path = "/upload123123"+java.io.File.separator+fileNameString; 
            file.saveAs(path); 
          } 
          Thread.sleep(100); 
          /*下載文件的語句 
          mySmartUpload.downloadFile("/路徑"+"文件名稱");*/ 
        } 
      }catch(Exception e){ 
        e.printStackTrace(); 
      } 
  }</span> 

相關(guān)文章

最新評論