使用commons-fileupload和commons-io上传文件和接收字段和参数(解决参数乱码)

之前遇到用commons-fileupload和commons-io 这两个包来接收表单的数据和文件 就出现各种乱码和接收不到参数的问题,最终参考了各种资料写出下面的可行的模板 这是注册商铺的流程大家可以用自己的业务去替换,

表单一定要要设置 method=“post” enctype=“multipart/form-data”

接收参数的时候可以用map来封装数据我直接写死了用switch来实现了,
前台:

<form id="shopform"  onsubmit="return false;" action="regshop" method="post"  enctype="multipart/form-data">
                    <h1>商铺注册</h1>
                    <br>
                    <div class="form-group">
                        <h4>店铺名</h4>

                        <input name="shop_name" type="text" class="form-control" id="exampleInputEmail1" placeholder="请输入要注册的店铺名">

                    </div>
                    <div class="form-group">
                        <h4>店铺地址</h4>
                        <input name="shop_address" type="text" class="form-control" id="exampleInputPassword1" placeholder="请输入你注册的店铺地址">

                    </div>
                    <div class="form-group">
                        <h4>店铺简介</h4>
                        <input  name="shop_value" type="text" class="form-control" id="exampleInputadress" placeholder="简介">
                    </div>
                    <div class="form-group">
                        <label for="inputfile"  style="font-size: 20px">上传图片</label>
                        <input type="file" id="inputfile" name="img">
                        <label style="font-size: 15px;padding-left: 100px">图片阅览</label>
                        <div id="imagg" style="height: 100px;width: 50px;margin-left: 100px;"></div>
                    </div>

                    <button type="submit" class="btn btn-default">注册</button>
                </form>

后台:

  response.setHeader("Pragma", "No-cache");
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-cache");
        JSONObject obj = new JSONObject();
        Shop shop1=null;
        String shop_name=null;
        String shop_address=null;
        String shop_value=null;
        String suffix=null;
        PrintWriter out= response.getWriter();
        //判断请求是否为multipart请求
        if(!ServletFileUpload.isMultipartContent(request)){
            throw new RuntimeException("当前请求不支持文件上传");
        }
        try {
            //创建一个FileItem工厂
            DiskFileItemFactory factory = new DiskFileItemFactory();
            //创建文件上传核心组件
            ServletFileUpload servletFileUpload = new ServletFileUpload(factory);
            //解析请求,获取到所有的item
            List<FileItem> items = servletFileUpload.parseRequest(request);
            //遍历items
            for(FileItem item : items){
                if(item.isFormField()){  //若item为普通表单项
                    String fieldName = item.getFieldName();  //获取表单项名称
                    String fieldValue = item.getString();  //获取表单项的值
                    fieldValue= new String(fieldValue.getBytes("ISO-8859-1"),"utf-8");
                    // new String(s.getBytes("ISO-8859-1"),"utf-8"); 转码 
                    //这里就是解决乱码问题的
                    switch (fieldName) {
                        case "shop_name":
                            shop_name = fieldValue;
                            shop1 = shopimpl.selectshopname(shop_name);
                            if (shop1!=null){
                                response.setStatus(300);
                                obj.put("status", 300);
                                obj.put("message", Const.HADSHOPNAME);
                            }
                            break;
                        case "shop_address":
                            shop_address=fieldValue;
                            break;
                        case "shop_value":
                            shop_value=fieldValue;
                            break;
                    }
                    System.out.println(fieldName+"="+fieldValue);
                }else{  //若item为文件表单项
                    //获取上传文件原始名称
                    String fileName = item.getName();
                    suffix="."+fileName.substring(fileName.lastIndexOf(".") + 1);
                    //获取输入流,其中有上传 文件的内容
                    InputStream is = item.getInputStream();
                    //获取文件保存在服务器的路径
                    String path = this.getServletContext().getRealPath("/images/shop");
                    //创建目标文件,将用来保存上传文件
                    File file = new File(path, shop_name+suffix);
                    //将输入流中的数据写入到输出流中
                    OutputStream os = new FileOutputStream(file);
                    //将输入流中的数据写入输出流中
                    int len = -1;
                    byte[] buf = new byte[1024];
                    while((len = is.read(buf)) != -1){
                        os.write(buf, 0, len);
                    }

                    os.close();
                    is.close();
                }
            }
        } catch (FileUploadException e) {
            e.printStackTrace();
        }
        if (shop1==null){
            Shop shop = new Shop();
            shop.setShop_name(shop_name);//设置店铺名字
            shop.setShop_address(shop_address);//设置店铺地址
            shop.setShop_value(shop_value);//设置店铺简介
            shop.setShop_img_url("images/shop/"+shop_name+suffix);//存放上传店铺图片的相对路径
            shop.setScore(9.0f);//默认9.0评分
            shopimpl.insert(shop);
            response.setStatus(200);
            obj.put("status", 200);
            obj.put("message", "店铺插入成功");


        }
        out.println(JSONObject.toJSON(obj));
        out.flush();
        out.close();

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值