yuypuyang
yuypuyang
2016-06-17 01:14

求助:文件上传的时候出现的问题,求大神帮忙

java.lang.NullPointerException: charsetName
33333333331
at java.lang.String.(String.java:414)
at java.lang.String.(String.java:481)
at org.apache.commons.fileupload.disk.DiskFileItem.getString(DiskFileItem.java:375)
at com.mumu.web.controller.ManagerServlet.addProduce(ManagerServlet.java:117)
at com.mumu.web.controller.ManagerServlet.doGet(ManagerServlet.java:36)
at com.mumu.web.controller.ManagerServlet.doPost(ManagerServlet.java:161)

            DiskFileItemFactory  factory= new DiskFileItemFactory();
        //上传文件
        System.out.println("22222222222222222222");
        ServletFileUpload upload = new ServletFileUpload(factory);
         upload.setHeaderEncoding("UTF-8");
        //请求解析
        List<FileItem> items = upload.parseRequest(request);
        for(FileItem item:items){
            if(item.isFormField()){// isFormField 为空或者不是对象
                //将数据封装到JavaBean中
                System.out.println("22222222225555555555");
                String fieldName = item.getFieldName();//字段名即Javabean的属性名,除了图片
                System.out.println("000000000000");
                String fieldValue = item.getString(request.getCharacterEncoding());
                System.out.println("99999999999");
                BeanUtils.setProperty(produce,fieldName,fieldValue);
            }else{  System.out.println("2222222666666666222");
                //处理文件上传
                InputStream in=item.getInputStream();
                String fileName = item.getName();
                fileName =UUID.randomUUID()+fileName.substring(fileName.lastIndexOf("\\")+1);
                //设置存取的文件名
                System.out.println("7777777772222222");
                produce.setProimg(fileName);
                OutputStream out = new FileOutputStream(storePath+"\\"+fileName);
                byte b[] = new byte[1024];
                int len=-1;
                while((len=in.read(b))!=-1){
                    out.write(b,0,len);
                }
                out.close();
                in.close();
                item.delete();//删除临时文件
            }
        }
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答

为你推荐