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;
}
}
新浪微博:http://t.sina.com.cn/androidguy 昵称:李宁_Lining