企业微信上传图片

"hidden" name="imgSrcs" id="imgSrcs">

"weui-cell">
              "weui-uploader__bd">
                   "weui-uploader__hd">
                      "weui-uploader__title">图片上传

                
                  "weui-uploader__files" id="uploaderFiles">
                    "weui-uploader__file" style="background-image:url(${base}/resources/shop/images/add.jpg)"id="add-img">
                        "" class="weui-uploader__file deleteFile" id="add-img1" hidden>
                        "" class="weui-uploader__file deleteFile" id="add-img2" hidden>
                        "" class="weui-uploader__file deleteFile" id="add-img3" hidden>
                        "" class="weui-uploader__file deleteFile" id="add-img4" hidden>
                        "" class="weui-uploader__file deleteFile" id="add-img5" hidden>
                    
                  
            
        

 "text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js">
  "text/javascript" src="${base}/resources/shop/js/jquery-weui.js">



//后台

/**
     * 上传路径
     */
    private final static String UPLOAD_REPAIR_IMG_URL = "upload/image/repair/";
    /**
     * 上传路径
     */
    private final static String UPLOAD_MESSAGE_IMG_URL = "upload/image/message/";
    /**
     * 上传路径
     */
    private final static String UPLOAD_APPRAISE_IMG_URL = "upload/image/appraise/";
/**
     * 上传图片
     * @author wxs
     * @version 1.0
     * @time 2017年10月17日 下午1:01:06
     */
    @RequestMapping(value = "/uploadImg", method = {RequestMethod.GET,RequestMethod.POST})
    public @ResponseBody
        Map uploadImg(String uploadStatus,HttpServletRequest request,RedirectAttributes redirectAttributes,String serverId) {
        Map data = new HashMap<>();
        //TODO
//        String openId = (String) request.getSession().getAttribute("openId");
        String openId = "wxs-hsd";
        WxUser wxUser = wxUserService.findByOpId(openId);
        //获取token
//        Admin admin = adminService.getCurrent();
//        Crop co=cropService.findByAdmin(admin.getId());
        Crop co=cropService.findByAdmin(9l);
        StringBuffer str = new StringBuffer();
        str.append("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=");
        str.append(co.getCropId());
        str.append("&corpsecret=");
        co.setSecret(co.getSecret());
        str.append(co.getSecret());
        String token=null;
        try{
            JSONObject jsontoken = JsonUtils.toJSONObject(HttpUtil.getInvoke(str.toString()));
            int errcode=jsontoken.getInt("errcode");
            if(errcode!=0){
                addFlashMessage(redirectAttributes, Message.error("cropid或secret不正确"));
                return data;
            }
            token = jsontoken.getString("access_token");
        }catch(JSONException e){
            addFlashMessage(redirectAttributes, Message.error("cropid或secret不正确"));
            return data;
        }
        //获取临时素材
        JSONObject json1=new JSONObject();
        json1.put("media_id", serverId);
        String getImg = "https://qyapi.weixin.qq.com/cgi-bin/media/get?access_token=" + token + "&media_id=" + serverId;
        //JSONObject getImgJson = JsonUtils.toJSONObject(HttpUtil.getInvoke(getImg));
        String imgSrc = "";
        try{
            URL u = new URL(getImg);
            HttpURLConnection conn = (HttpURLConnection) u.openConnection();
            conn.setRequestMethod("GET");
            conn.connect();
            BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
            // 生成不同文件名称
            String path = request.getSession().getServletContext().getRealPath("/");
            String baseUploadPath = "";
            String newFileName = UUID.randomUUID().toString() + ".jpg";
            if("repair".equals(uploadStatus)){
                baseUploadPath = path + UPLOAD_REPAIR_IMG_URL;
                imgSrc = "/" + UPLOAD_REPAIR_IMG_URL + newFileName;
            }
            if("appraise".equals(uploadStatus)){
                baseUploadPath = path + UPLOAD_APPRAISE_IMG_URL;
                imgSrc = "/" + UPLOAD_APPRAISE_IMG_URL + newFileName;
            }
            if("message".equals(uploadStatus)){
                baseUploadPath = path + UPLOAD_MESSAGE_IMG_URL;
                imgSrc = "/" + UPLOAD_MESSAGE_IMG_URL + newFileName;
            }
//            String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
            data.put("imgSrc", imgSrc);
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(baseUploadPath + newFileName));
            byte[] buf = new byte[1024];
            int length = bis.read(buf);
            while (length != -1) {
                bos.write(buf, 0, length);
                length = bis.read(buf);
            }
            bos.close();
            bis.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return data;
    }

"hidden" name="imgSrcs" id="imgSrcs">

"weui-cell">
"weui-uploader__bd">
"weui-uploader__hd">

"weui-uploader__title">图片上传

    "weui-uploader__files" id="uploaderFiles">
  • "weui-uploader__file" style="background-image:url(${base}/resources/shop/images/add.jpg)"id="add-img">
  • "" class="weui-uploader__file deleteFile" id="add-img1" hidden>
    "" class="weui-uploader__file deleteFile" id="add-img2" hidden>
    "" class="weui-uploader__file deleteFile" id="add-img3" hidden>
    "" class="weui-uploader__file deleteFile" id="add-img4" hidden>
    "" class="weui-uploader__file deleteFile" id="add-img5" hidden>





    //后台

    /**
    * 上传路径
    */
    private final static String UPLOAD_REPAIR_IMG_URL = "upload/image/repair/";
    /**
    * 上传路径
    */
    private final static String UPLOAD_MESSAGE_IMG_URL = "upload/image/message/";
    /**
    * 上传路径
    */
    private final static String UPLOAD_APPRAISE_IMG_URL = "upload/image/appraise/";
    /**
    * 上传图片
    * @author wxs
    * @version 1.0
    * @time 2017年10月17日 下午1:01:06
    */
    @RequestMapping(value = "/uploadImg", method = {RequestMethod.GET,RequestMethod.POST})
    public @ResponseBody
    Map uploadImg(String uploadStatus,HttpServletRequest request,RedirectAttributes redirectAttributes,String serverId) {
    Map data = new HashMap<>();
    //TODO
    // String openId = (String) request.getSession().getAttribute("openId");
    String openId = "wxs-hsd";
    WxUser wxUser = wxUserService.findByOpId(openId);
    //获取token
    // Admin admin = adminService.getCurrent();
    // Crop co=cropService.findByAdmin(admin.getId());
    Crop co=cropService.findByAdmin(9l);
    StringBuffer str = new StringBuffer();
    str.append("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=");
    str.append(co.getCropId());
    str.append("&corpsecret=");
    co.setSecret(co.getSecret());
    str.append(co.getSecret());
    String token=null;
    try{
    JSONObject jsontoken = JsonUtils.toJSONObject(HttpUtil.getInvoke(str.toString()));
    int errcode=jsontoken.getInt("errcode");
    if(errcode!=0){
    addFlashMessage(redirectAttributes, Message.error("cropid或secret不正确"));
    return data;
    }
    token = jsontoken.getString("access_token");
    }catch(JSONException e){
    addFlashMessage(redirectAttributes, Message.error("cropid或secret不正确"));
    return data;
    }
    //获取临时素材
    JSONObject json1=new JSONObject();
    json1.put("media_id", serverId);
    String getImg = "https://qyapi.weixin.qq.com/cgi-bin/media/get?access_token=" + token + "&media_id=" + serverId;
    //JSONObject getImgJson = JsonUtils.toJSONObject(HttpUtil.getInvoke(getImg));
    String imgSrc = "";
    try{
    URL u = new URL(getImg);
    HttpURLConnection conn = (HttpURLConnection) u.openConnection();
    conn.setRequestMethod("GET");
    conn.connect();
    BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
    // 生成不同文件名称
    String path = request.getSession().getServletContext().getRealPath("/");
    String baseUploadPath = "";
    String newFileName = UUID.randomUUID().toString() + ".jpg";
    if("repair".equals(uploadStatus)){
    baseUploadPath = path + UPLOAD_REPAIR_IMG_URL;
    imgSrc = "/" + UPLOAD_REPAIR_IMG_URL + newFileName;
    }
    if("appraise".equals(uploadStatus)){
    baseUploadPath = path + UPLOAD_APPRAISE_IMG_URL;
    imgSrc = "/" + UPLOAD_APPRAISE_IMG_URL + newFileName;
    }
    if("message".equals(uploadStatus)){
    baseUploadPath = path + UPLOAD_MESSAGE_IMG_URL;
    imgSrc = "/" + UPLOAD_MESSAGE_IMG_URL + newFileName;
    }
    // String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
    data.put("imgSrc", imgSrc);
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(baseUploadPath + newFileName));
    byte[] buf = new byte[1024];
    int length = bis.read(buf);
    while (length != -1) {
    bos.write(buf, 0, length);
    length = bis.read(buf);
    }
    bos.close();
    bis.close();
    } catch (MalformedURLException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    return data;
    }

    你可能感兴趣的:(企业微信上传图片)