开发者社区> 问答> 正文

钉钉上传文件接口失败,-1 系统繁忙,求助


java测试代码如下:


public String uploadMedia2(String cropId, String url, File file)
            throws Exception {
        String mediaId = "";
        HttpPost httpPost = new HttpPost(url);
        Closeable HttpResponse response = null;
        Closeable HttpClient httpClient = HttpClients.createDefault();
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(2000).setConnectTimeout(2000).build();
        httpPost.setConfig(requestConfig);
        HttpEntity requestEntity = MultipartEntityBuilder
                .create()
                .addPart(
                        "media",
                        new FileBody(file,
                                ContentType.APPLICATION_OCTET_STREAM, file
                                        .getName())).build();
        httpPost.setEntity(requestEntity);
        try {
            response = httpClient.execute(httpPost, new BasicHttpContext());
            if (response.getStatusLine().getStatusCode() != 200) {
                return null;
            }
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                String resultStr = EntityUtils.toString(entity, "utf-8");
                JsonMapper jsonMapper = new JsonMapper();
                Map<String, String> result = jsonMapper.fromJson(resultStr,
                        Map.class);
                if (result.get("errcode") == "0") {
                    // 成功
                    result.remove("errcode");
                    result.remove("errmsg");
                    mediaId = result.get("media_id");
                    return mediaId;
                } else {
                    String errMsg = (String) result.get("errmsg");
                    throw new Exception(errMsg);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (response != null)
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
        return null;
    }

展开
收起
2016-10-20 17:04:24 13134 0
3 条回答
写回答
取消 提交回答
  • 回 1楼钉钉-久铖的帖子
    谢谢,我的也解决了

    -------------------------

    回 2楼imoonstal的帖子
    谢谢,已经解决了,还有个问题,请问知道吗?
    下载文件时,不管上传上去的事什么类型的,下载下来都是XXXX.file文件,知道在哪获取到文件类型转换吗?
    2016-10-21 16:02:48
    赞同 展开评论 打赏
  • Re钉钉上传文件接口失败,-1 系统繁忙,求助
    下面这段代码是一个做java的朋友给我,说可以,你试试。的换成e---Http,---去掉。这个词禁止发表,哎
    另外.net版本的这个问题我也解决了。我靠,不让我发地址。
    终于够5贴了
    http://www.cnblogs.com/imoonstal/p/5984205.html



    public static JSONObject uploadMedia(String url, File file) {
            Closeabl的Response response = null;
            Closeabl的Client httpClient = getHttpClient();
            if (httpClient == null) {
                return null;
            }

            HttpPost httpPost = new HttpPost(url);
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();
            httpPost.setConfig(requestConfig);

            HttpEntity requestEntity = MultipartEntityBuilder.create().addPart("media",
                                                                               new FileBody(
                                                                                            file,
                                                                                            ContentType.APPLICATION_OCTET_STREAM,
                                                                                            file.getName())).build();
            httpPost.setEntity(requestEntity);

            try {
                response = httpClient.execute(httpPost, new BasicHttpContext());

                if (response.getStatusLine().getStatusCode() != 200) {

                    System.out.println("request url failed, http code=" + response.getStatusLine().getStatusCode()
                                       + ", url=" + url);
                    return null;
                }
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    String resultStr = EntityUtils.toString(entity, "utf-8");

                    JSONObject result = JSON.parseObject(resultStr);
                    if (result.getInteger("errcode") == 0) {
                        return result;
                    } else {
                        System.out.println("request url=" + url + ",return value=");
                        System.out.println(resultStr);
                    }
                }
            } catch (Exception e) {
                System.out.println("request url=" + url + ", exception, msg=" + e.getMessage());
                e.printStackTrace();
            } finally {
                if (response != null) try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            return null;
        }


    -------------------------

    Re回 2楼imoonstal的帖子
    引用第4楼蘇于2016-10-21 16:04发表的 回 2楼imoonstal的帖子 :
    谢谢,已经解决了,还有个问题,请问知道吗?
    下载文件时,不管上传上去的事什么类型的,下载下来都是XXXX.file文件,知道在哪获取到文件类型转换吗? [url=https://bbs.aliyun.com/job.php?action=topost&tid=297317&pid=839937][/url]

    头文件里有你上传时的文件名。这个是有工具获取的头。
    [attachment=114688]

    我是.net开发的,头是这样取的
    [attachment=114690]
    2016-10-21 13:13:15
    赞同 展开评论 打赏
  • Re钉钉上传文件接口失败,-1 系统繁忙,求助
    请提供一下时间点,当时请求的参数用于排查。access_token 因为安全问题请你提供末位16位之前的内容
    2016-10-20 20:09:43
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
钉钉小程序——为工作方式插上翅膀 立即下载
钉钉客户端自动化的应用 立即下载
使命必达 —— 钉钉企业级 消息服务的机遇与挑战 立即下载