java上传

2017-04-27     浏览次数:

/**
     * 修改个人信息页面
     * @param request
     * @param response
     */
    private void changeUser(HttpServletRequest request, HttpServletResponse response) throws Exception{
        DiskFileItemFactory factory=new DiskFileItemFactory();//创建磁盘工厂
        //ServletFileUpload 处理表单数据,将数据封装到 FileItem 对象中。
        //上传文件的所有数据都在FileItem中
        ServletFileUpload upload=new ServletFileUpload(factory);
        //使用集合接收,因为上传的可能不仅仅是一个文件
        List<FileItem> items=upload.parseRequest(request);//获取所有的上传域列表;
        // 解析request请求中的数据
        Iterator<FileItem> itr=items.iterator();
        User user=(User) request.getSession().getAttribute("currentUser");
        boolean imageChange=false;
        // 对集合数据进行遍历
        while(itr.hasNext()){
            //依次取出数据放到文件对象中
            FileItem item=(FileItem)itr.next();
            //普通表单元素
            if(item.isFormField()){
                String fieldName=item.getFieldName();//获取表单值
                if(fieldName.equals("nickName")){
                    // 以指定的编码保存昵称数据
                    user.setNickName(item.getString("utf-8").trim());
                }else if(fieldName.equals("mood")){
                    // 以指定的编码保存心情数据
                    user.setMood(item.getString("utf-8").trim());
                }
            }else if(!"".equals(item.getName())){ // 获取上传文件的字段名
                try{
                    imageChange=true;
                    //使用格式化的时间当图片名称
                    /*String imageName=DateUtil.getCurrentDateStr();
                    String filePath1=PropertiesUtil.getValue("imagePath")+user.getImageName();
                    user.setImageName(imageName+"."+item.getName().split("\\.")[1]);
                    String filePath=PropertiesUtil.getValue("imagePath")+imageName+"."+item.getName().split("\\.")[1];
                    item.write(new File(filePath));
                    (new File(filePath1)).delete();
                    Thread.sleep(5000);*/
                     
                    String imageName=DateUtil.getCurrentDateStr();
                    String realPath=getServletContext().getRealPath("/userImages");
//                  System.out.println("路径"+realPath);
                    String imageFile=imageName+"."+item.getName().split("\\.")[1];
                    File saveFile1=new File(realPath,user.getImageName());
                    user.setImageName(imageFile);//为用户设置头像
//                  System.out.println(imageFile);
                    //创建指定路径
                    File saveFile=new File(realPath,imageFile);
                    item.write(saveFile);
                    saveFile1.delete();
//                  Thread.sleep(5000);
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
         
        if(!imageChange){
            // replaceFirst, 替换第一个子字符串
            //如果图片没有修改,设置为默认图片
            user.setImageName(user.getImageName().replaceFirst(PropertiesUtil.getValue("imageFile"), ""));
//          System.out.println(PropertiesUtil.getValue("imageFile"));
        }
         
        try {
            int saveNums=(new UserDaoImpl()).updateuser(user);
            if(saveNums>0){
            //  user.setImageName(PropertiesUtil.getValue("imageFile")+user.getImageName());
                user.setImageName(user.getImageName());
                request.getSession().setAttribute("currentUser", user);
                request.getRequestDispatcher("main?action=mainPage").forward(request, response);
            }else{
                request.setAttribute("currentUser", user);
                request.setAttribute("error", "更新用户信息失败");
                request.setAttribute("mainPage", "user/userSave.jsp");
                request.getRequestDispatcher("mainTemp.jsp").forward(request, response);
            }
        } catch (Exception e) {
             
            e.printStackTrace();
        }
         
         
    }
*转载请注明出处,更多资料尽在 吾爱编程
返回吾爱编程首页