基于网上很多人利用新浪api开发新浪微博客户端的时候遇到无法发图片的问题,很多人卡在了这一布。现将代码呈上,希望能帮到一些朋友。

/**
     * 发表带图片的微博
     * @param token
     * @param tokenSecret
     * @param aFile
     * @param status
     * @param urlPath
     * @return
     */
    public String uploadStatus(String token, String tokenSecret, File aFile, String status, String urlPath) {
        httpOAuthConsumer = new DefaultOAuthConsumer(consumerKey,consumerSecret);
        httpOAuthConsumer.setTokenWithSecret(token,tokenSecret);
        String result = null;
        try {
            URL url = new URL(urlPath);
            HttpURLConnection request = (HttpURLConnection) url.openConnection();
            request.setDoOutput(true);
            request.setRequestMethod("POST");
            HttpParameters para = new HttpParameters();
            para.put("status", URLEncoder.encode(status,"utf-8").replaceAll("\\+", "%20"));
            String boundary = "---------------------------37531613912423";
            String content = "--"+boundary+"\r\nContent-Disposition: form-data; name=\"status\"\r\n\r\n";
            String pic = "\r\n--"+boundary+"\r\nContent-Disposition: form-data; name=\"pic\"; filename=\"image.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n";女装品牌排行榜
        } catch (OAuthMessageSignerException e) {
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            e.printStackTrace();
        } catch (OAuthCommunicationException e) {
            e.printStackTrace();
        }
        return result;
    }

相关文章:

  • 2021-10-11
  • 2021-09-17
  • 2022-12-23
  • 2022-01-28
  • 2022-12-23
  • 2021-12-27
  • 2021-09-24
  • 2022-02-08
猜你喜欢
  • 2022-12-23
  • 2021-09-09
  • 2022-03-07
  • 2021-06-11
  • 2022-01-17
  • 2022-12-23
  • 2022-01-13
相关资源
相似解决方案