使用表单提交并提交页面上传图面

使用表单提交并提交页面上传图面

话不多说,直接上代码

  /**
         * 增加商品
         */
        else if("add".equals(action)){
            Good good1=new Good();
            //接收增加的商品信息,并调用后台方法,将商品信息插入数据库
            String uploadFileName = "";
            String fieldName = "";
            //good=new Good();
            //解析请求之前先判断请求类型是否为文件上传类型/
            boolean isMultipart = ServletFileUpload.isMultipartContent(request);
            //指定上传位置
            String uploadFilePath = request.getSession().getServletContext().getRealPath("GoodsImage/");
            File saveDir = new File(uploadFilePath);
            //如果目录不存在,就创建目录
            if(!saveDir.exists()){
                saveDir.mkdir();
            }
            if(isMultipart){

                //创建文件上传核心类
                FileItemFactory factory = new DiskFileItemFactory(); // 实例化一个硬盘文件工厂,用来配置上传组件ServletFileUpload
                ServletFileUpload upload = new ServletFileUpload(factory); // 用以上工厂实例化上传组件
                try{
                    //处理表单请求
                    List<FileItem> items = upload.parseRequest(request);
                    Iterator<FileItem> iter = items.iterator();
                        while(iter.hasNext()){
                        FileItem item = (FileItem)iter.next();
                        if(item.isFormField()){// 如果是普通表单控件
                            fieldName = item.getFieldName();// 获得该字段名称
                            if(fieldName.equals("goodsName")){
                                good1.setGoodsName(item.getString("UTF-8"));//获得该字段值
                            }else if(fieldName.equals("goodsSmalId")){  //goodsSmalld是前端JSP页面传过来的参数,下面的都是
                                good1.setGoodsSmalId(Integer.parseInt(item.getString("UTF-8")));
                            }else if(fieldName.equals("goodsMoney")){
                                good1.setGoodsMoney(Double.parseDouble(item.getString("UTF-8")));
                            }else if(fieldName.equals("goodsNumber")){
                                good1.setGoodsNumber(Integer.parseInt(item.getString("UTF-8")));
                            }else if(fieldName.equals("goodsCarriage")){
                                good1.setGoodsCarriage(Double.parseDouble(item.getString("UTF-8")));
                            }else if(fieldName.equals("goodsType")){
                                good1.setGoodsType(Integer.parseInt(item.getString("UTF-8")));
                            }else if(fieldName.equals("goodsSeId")){
                                good1.setGoodsSeId(Integer.parseInt(item.getString("UTF-8")));
                            }else if(fieldName.equals("goodsDiscId")){
                                good1.setGoodsDiscId(Integer.parseInt(item.getString("UTF-8")));
                            }
                        }else{// 如果为文件域
                            String fileName = item.getName();// 获得文件名(全路径)
                            if(fileName != null && !fileName.equals("")){
                                File fullFile = new File(fileName);
                                File saveFile = new File(uploadFilePath,fullFile.getName());//将文件保存到指定的路径
                                item.write(saveFile);
                                uploadFileName = fullFile.getName();
                                good1.setGoodsImage(uploadFileName);
                            }
                        }
                    }
                }catch(Exception e){
                    e.printStackTrace();
                }

            }
            System.out.println("上传成功之后的文件名:" + good1.getGoodsImage());
            //调用后台的方法,将信息插入数据库中
            int n= goodsQueryService.addGoods(good1);
           boolean flag=false;
            if (n>0) {
                out.write("<script type='text/javascript'>");
                out.write("alert('增加成功!');");
                out.write("location.href = \"/page/goods/goodsQuery.jsp\";");
                out.write("</script>");
            } else {
                out.write("<script type='text/javascript'>");
                out.write("alert('增加失败!');");
                out.write("location.href = \"/page/goods/goodsQuery.jsp\";");
                out.write("</script>");
            }
            out.write("{\"flag\":\"" + flag + "\"}");
        }
        out.flush();
        out.close();

猜你喜欢

转载自blog.csdn.net/qq_40948117/article/details/89512963