Package weibo4j.examples.timeline

Source Code of weibo4j.examples.timeline.Upload

package weibo4j.examples.timeline;

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

import weibo4j.Timeline;
import weibo4j.http.ImageItem;
import weibo4j.model.Status;

public class Upload {
  public static void main(String args[]) {
    try {
      try {
        byte[] content = readFileImage(args[2]);
        System.out.println("content length:" + content.length);
        ImageItem pic = new ImageItem("pic", content);
        String s = java.net.URLEncoder.encode(args[1], "utf-8");
        Timeline tl = new Timeline();
        tl.client.setToken(args[0]);// access_token
        Status status = tl.UploadStatus(s, pic);

        System.out.println("Successfully upload the status to [" + status.getText() + "].");
      } catch (Exception e1) {
        e1.printStackTrace();
      }
    } catch (Exception ioe) {
      System.out.println("Failed to read the system input.");
    }
  }

  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;
  }
}
TOP

Related Classes of weibo4j.examples.timeline.Upload

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.