java 實現圖片上傳功能

1:jsp 頁面上傳圖片按鈕在這裏我就寫相關的代碼服務器

        <div class="control-group">
         <label class="control-label">營業執照:</label>
             <div class="controls">
                <input type="file" class="form-control-input" name="yingYeZhiZhaoName"  value="${office.yingYeZhiZhao }"/>
                <%-- <input type="hidden" name="yingYeZhiZhao" value="${office.yingYeZhiZhao }"/> --%>
            </div> 
            <div class="controls"><img src="${office.yingYeZhiZhao }" width="203" title="點擊更換圖片" id="logoImgTag" /></div> 
        </div>

2:下面是控制層action 代碼app

@RequestMapping("/add")
    public String addOffice(HttpServletRequest request, HttpServletResponse response,Office office, Model model, RedirectAttributes redirectAttributes, User user,String officeId,String userId,String roleName,MultipartFile yingYeZhiZhaoName) {
        String originalFilename = yingYeZhiZhaoName.getOriginalFilename();
        if((officeId !=null && !"".equals(officeId))&&(userId !=null && !"".equals(userId)) ){//修改用戶信息
            User newUser = systemService.getUser(userId);
            Office newOffice = officeService.get(officeId);
            
            //用戶表基本信息
                if(StringUtils.isNotBlank(user.getPassword())){
                    String password = SystemService.entryptPassword(user.getPassword());
                    newUser.setPassword(password);
                }
                //newUser.setName(user.getName());//公司名稱
                
                //公司表信息
                newOffice.setMaster(office.getMaster());
                newOffice.setMasterSex(office.getMasterSex());
                newOffice.setZhiWei(office.getZhiWei());
                newOffice.setEmail(office.getEmail());//公司郵箱
                newOffice.setName(office.getName());//公司名稱
                newOffice.setJianCheng(office.getJianCheng());//公司簡稱
                newOffice.setProvince(office.getProvince());//
                newOffice.setCity(office.getCity());//
                newOffice.setAddress(office.getAddress());
                newOffice.setDuiGongZhangHu(office.getDuiGongZhangHu());//對公帳戶
                newOffice.setDuiGongShouKuan(office.getDuiGongShouKuan());//對公收款
                newOffice.setDuiGongYinHang(office.getDuiGongYinHang());//對公銀行
                newOffice.setQuHao(office.getQuHao());
                newOffice.setTelephone(office.getTelephone());//公司固話
                newOffice.setRemarks(office.getRemarks());//備註
            
                if(originalFilename !=null && !"".equals(originalFilename)){//若是是從新上傳了圖片
                    int num=(int)(Math.random()*100000);
                    Map<String, String> map = QiniuUtils.fileUploadPdf(yingYeZhiZhaoName,num+officeId);
                    newOffice.setYingYeZhiZhao(map.get(Constants.QIUNIU_EXHI_IMAGE_SOURCE));
                }
                officeService.updateSysOffice(newOffice);
                systemService.udateUser(newUser);
              //officeService.save(newOffice);
                
        }else{//添加用戶信息
            
            if(originalFilename !=null && !"".equals(originalFilename)){//若是是從新上傳了圖片
                int num=(int)(Math.random()*100000);
                Map<String, String> map = QiniuUtils.fileUploadPdf(yingYeZhiZhaoName,num+officeId);
                office.setYingYeZhiZhao(map.get(Constants.QIUNIU_EXHI_IMAGE_SOURCE));
            }
            List<Role> roles = new ArrayList<Role>(0);
            Role role = null;
            User newUser = new User();
            newUser.setPassword(SystemService.entryptPassword(user.getPassword()));// 設置密碼
            newUser.setMobile(user.getMobile());// 手機號碼
            newUser.setLoginName(user.getMobile());// 登陸名(手機或者郵箱)
            
            role = systemService.getRole(roleName);//這地方判斷是2是會展代理商15是會展主辦方
            roles.add(role);

            Role admin = new Role();
            admin.setId(Constants.HE_ZUO_SHANG_ADMIN);// 默認註冊的用戶就是合做商管理員
            roles.add(admin);
            newUser.setRoleList(roles);

            Area area = new Area();
            area.setId("1");
            office.setCode(UUID.randomUUID().toString());
            office.setType("1");
            office.setGrade("1");
            office.setUseable("1");
            office.setDelFlag("0");
            User createBy = new User();
            createBy.setId("1");
            Office poffice = new Office();
            poffice.setId("0");
            office.setParent(poffice);
            office.setParentIds("0,");
            office.setArea(area);
            office.preInsert();
            office.setIsNewRecord(true);
            office.setCreateBy(createBy);
            office.setUpdateBy(createBy);
            office.setRole(role);
            officeService.register(office);
            newUser.setOffice(office);
            newUser.setCompany(office);
            newUser.setName(office.getName());
            systemService.saveUser(newUser);
            
        }
        
        String id = "0".equals(office.getParentId()) ? "" : office.getParentId();
        return "redirect:" + adminPath + "/sys/office/list?id=" + id + "&parentIds=" + office.getParentIds();
    }

以上實現圖片功能是把圖片上傳到七牛服務器上dom

注意jsp

<input type="file" class="form-control-input" name="yingYeZhiZhaoName"  value="${office.yingYeZhiZhao }"/> 控件裏面的name 不要和提交時表單對象裏面的屬性同樣,因此這地方最好不要寫對象屬性的名字
3:下面是七牛部分代碼
/**
     * 七牛招展函上傳
     * @param file
     * @param name
     * @return
     */
    public static Map<String, String> fileUploadPdf(MultipartFile file, String name) {
        Map<String, String> map = new HashMap<String, String>();
        try {
            UploadManager uploadManager = new UploadManager();

            // 上傳到雲中的文件名
            // String newName = name+System.currentTimeMillis();

            byte[] bytes = file.getBytes();

            uploadManager.put(bytes, name, getUpToken0(name));
            String isProduct = Global.getConfig("isProduct");
            String url = null;
            if ("Y".equalsIgnoreCase(isProduct)) {
                url = Constants.QIUNIU_EXHI_IMAGE_URL;
            } else if ("N".equalsIgnoreCase(isProduct)) {
                url = Constants.QIUNIU_TEST_IMAGE_URL;
            }
            map.put("fileUrl", url + name);
            map.put("disposeUrl", url + name + ":result");
        } catch (Exception e) {
            log.error("七牛上傳文件失敗:" + e.getMessage());
            e.printStackTrace();
        }
        return map;
    }
相關文章
相關標籤/搜索