隨筆 - 312, 文章 - 14, 評論 - 1393, 引用 - 0
          數據加載中……

          向sina博博發圖像

          package twitter4j.examples;

          import java.io.BufferedInputStream;
          import java.io.FileInputStream;
          import java.io.IOException;

          import twitter4j.Status;
          import twitter4j.Twitter;
          import twitter4j.http.ImageItem;

          public class OauthUpload {
              /**
               * Usage: java -Dtwitter4j.oauth.consumerKey=[consumer key] -Dtwitter4j.oauth.consumerSecret=[consumer secret] twitter4j.examples.OAuthUpload [accessToken] [accessSecret] [imageFilePath]
               * @param args message
               */
              public static void main(String[] args) {
                  try {
                      System.setProperty("twitter4j.oauth.consumerKey", Twitter.CONSUMER_KEY);
                      System.setProperty("twitter4j.oauth.consumerSecret", Twitter.CONSUMER_SECRET);
                      
                      Twitter twitter = new Twitter();
                      twitter.setToken("正確的token", "正確的secret");
                         twitter.updateStatus("aaa");  //  此條語句可以發成功
                       try {
                               byte[] content=readFileImage("e:\\small_tiger.jpg");
                               System.out.println("content length:"+content.length);
                               ImageItem pic=new ImageItem("aaa",content);
                               //  無法成功發送圖像
                              Status status = twitter.uploadStatus("第一個帶照片的微博",pic);
                              
                              //System.out.println("Successfully upload the status to [" + status.getText() + "].");
                          } catch (Exception e1) {
                              // TODO Auto-generated catch block
                              e1.printStackTrace();
                          }
                      System.exit(0);
                  } catch (Exception ioe) {
                      System.out.println("Failed to read the system input.");
                      System.exit( -1);
                  }
              }
              
              public static byte[] readFileImage(String filename) throws IOException {
                  BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream(filename));
                  int len=bufferedInputStream.available();
                  byte[] bytes=new byte[len];
                  int r=bufferedInputStream.read(bytes);
                  if(len!=r)
                  {
                    bytes=null;
                    throw new IOException("讀取文件不正確");
                  }
                  bufferedInputStream.close();
                  return bytes;
              }

          }



          Android開發完全講義(第2版)(本書版權已輸出到臺灣)

          http://product.dangdang.com/product.aspx?product_id=22741502



          Android高薪之路:Android程序員面試寶典 http://book.360buy.com/10970314.html


          新浪微博:http://t.sina.com.cn/androidguy   昵稱:李寧_Lining

          posted on 2010-03-24 13:30 銀河使者 閱讀(491) 評論(1)  編輯  收藏

          評論

          # re: 向sina博博發圖像  回復  更多評論   

          這個問題我也遇到過,是轉碼的問題
          把你要發的中文轉碼之后再發就行了
          String msg = URLEncoder.encode("生活", "UTF-8");
          2010-12-29 14:31 | ldf714

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 天津市| 平和县| 普宁市| 辰溪县| 洞头县| 井研县| 西林县| 清河县| 平原县| 西吉县| 峡江县| 石台县| 鹿邑县| 建阳市| 慈溪市| 吉林省| 唐山市| 抚顺县| 澎湖县| 南郑县| 仙桃市| 全南县| 麻阳| 遂宁市| 福安市| 阿瓦提县| 隆昌县| 修水县| 天全县| 体育| 连平县| 饶河县| 嘉兴市| 拜城县| 开封市| 综艺| 林州市| 南江县| 广西| 板桥市| 西盟|