request.getParameter("key")拿不到值的问题

时间:2024-03-28 07:17:49

今天在写一个form表单的时候,提交到后台之后发现使用request.getParameter()拿到的值全部为null

错误原因是这个form表单中有一个文件(图片)上传的项,form表单中有一个属性enctype="multipart/form-data"。

<form name="articleform" class="form-horizontal" action="${pageContext.request.contextPath}/list?method=updateArticle"
                  method="post" enctype="multipart/form-data">

查了一下这个属性

request.getParameter("key")拿不到值的问题

此时用request是拿不到这个值的,因为这个表单是基于流的方式提交的。

此时需要导入下面三个依赖,此处我使用的maven方式引入

<dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.2</version>
    </dependency>

    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.4</version>
    </dependency>

    <dependency>
      <groupId>commons-beanutils</groupId>
      <artifactId>commons-beanutils</artifactId>
      <version>1.8.0</version>
    </dependency>

具体的实现方式如下

private void updateArticle() throws ServletException, IOException{
        Article article = new Article();
        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        List items = null;
        try {
            items = upload.parseRequest(request);
        } catch (FileUploadException e) {
            e.printStackTrace();
        }
        Iterator iter = items.iterator();
        String picUrl = null;
        while (iter.hasNext()) {
            FileItem item = (FileItem) iter.next();
            if(item.isFormField()){//判断是非文件上传
                String value = item.getString("UTF-8");
                //分枝结构匹配上传项
                switch (item.getFieldName()) {
                    case "id":
                        String id = value;
                        article.setId(Integer.parseInt(id));
                        break;
                    case "picUrl":
                        picUrl = value;
                        article.setImage(picUrl);
                        break;
                    case "code":
                        String code = value;
                        ArticleType type = new ArticleType();
                        type.setCode(code);
                        article.setArticleType(type);
                        break;
                    case "titleStr":
                        String title = value;
                        article.setTitle(title);
                        break;
                    case "supplier":
                        String supplier = value;
                        article.setSupplier(supplier);
                        break;
                    case "locality":
                        String locality = value;
                        article.setLocality(locality);
                        break;
                    case "price":
                        String price = value;
                        article.setPrice(Double.parseDouble(price));
                        break;
                    case "storage":
                        String storage = value;
                        article.setStorage(Integer.parseInt(storage));
                        break;
                    case "description":
                        String description = value;
                        article.setDescription(description);
                        break;
                }
            }else{
                //得到文件保存路径
                String sysPath = request.getServletContext().getRealPath("/resources/images/article/");
                //得到上传问价的文件名  test.jpg
                String filename = item.getName();
                if (null == filename || filename == "") {
                    filename = picUrl;
                }
                sysPath = sysPath + filename;
                article.setImage(filename);
                File saveFile = new File(sysPath);
                try {
                    item.write(saveFile);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        shopService.updateArticle(article);
        request.setAttribute("tip","修改商品成功");
        showUpdate();
    }

这样就可以拿到form表单上传的数据了,并且完成上传文件