form表单同时提交文件类型的input和普通input的方法

废话不多说,直接奔主题
大家都知道,当form表单中既要提交文本,又要上传图片时,必需在form表单中加上属性enctype ="multipart/form-data",但一加上这个属性,用request.getParameter("name的值")是无法获取的,怎办呢?
只能用fileupload的提供的方法了,我的例子如下:
我的form表单:
<formaction="<%=request.getContextPath()%>/servlet/addproductServlet"method="post" enctype = "multipart/form-data">
    产品小图片:
   <input type="file"name="fileupS"/><br/>
    产品大图片:
   <input type="file"name="fileupB"/><br/>
   产品所属类别:
   <select name="sort">
     <optionvalue="">--选择产品类别--</option>
     <optionvalue="a">a</option>
     <optionvalue="b">b</option>
     <optionvalue="c">c</option>
     <optionvalue="d">d</option>
   </select><br/>
    产品名称:
   <input type="text" name="productName"/><br>
    产品描述:
   <textarea rows="3" cols="40" name="productDes"></textarea><br>   
   <input type="submit" value="提交"/><br/>
  </form>
获取其中的值并上传文件如下:
String productName=null,productDes=null,sort=null,fileupS=null,fileupB=null;
      DiskFileItemFactory dff = new DiskFileItemFactory();
      ServletFileUpload fu = new ServletFileUpload(dff);
      
       try{   
          List li =fu.parseRequest(request);
          Iteratoriter = li.iterator();
         while(iter.hasNext()){              
             FileItemitem = (FileItem)iter.next();
            if(item.isFormField()){  //此处是判断非文件域,即不是<inputtype="file"/>的标签
                String name=item.getFieldName(); //获取form表单中name的id
               if("productName".equals(name))  
                  productName=item.getString("utf-8"); //item是指定id的value值,此处用      item.getString("utf-8")是把item用utf-8解析,根据你的需要可以用其他的,如:gbk;
                elseif("productDes".equals(name))
                  productDes=item.getString("utf-8");
                elseif("sort".equals(name))
                  sort=item.getString("utf-8");            
            }else{          //如果是文件域,则上传文件
                String fName= item.getName();  //这里是获取文件名

                 int i =fName.lastIndexOf("\\");//此处由于ie中上传文件时是以图片的绝对路径全称作为文件名所以必需截取后面的文件名
                 fName =fName.substring(i+1,fName.length());

                Stringfilepath = request.getRealPath("/")+"product";
                File path =new File(filepath);
               if(!path.isDirectory()){ path.mkdir();  }
                try{
                  item.write(new File(path+"\\"+fName));                  
                  if(fName!="") {item.write(new File(path+"\\"+fName));}
                  if("fileupS".equals(item.getFieldName())){fileupS =fName;}
                   elseif("fileupB".equals(item.getFieldName())){fileupB =fName;}         
                } catch(Exception e) {
                  e.printStackTrace();
                             
            }
         }
       } catch(FileUploadException e1) {
         e1.printStackTrace();
      }
重要细节:<input "file"name="xxx"/>中一定要有name属性,否则取不到file数据!!!!!!!!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值