-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUploadPic.java
77 lines (66 loc) · 1.92 KB
/
UploadPic.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package info.caq9;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import weibo4j.Timeline;
import weibo4j.Weibo;
import weibo4j.http.ImageItem;
import weibo4j.model.WeiboException;
public class UploadPic {
// Returns the contents of the file in a byte array.
public static byte[] getBytesFromFile(File file) throws IOException {
InputStream is = new FileInputStream(file);
// Get the size of the file
long length = file.length();
// You cannot create an array using a long type.
// It needs to be an int type.
// Before converting to an int type, check
// to ensure that file is not larger than Integer.MAX_VALUE.
if (length > Integer.MAX_VALUE) {
// File is too large
}
// Create the byte array to hold the data
byte[] bytes = new byte[(int) length];
// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += numRead;
}
// Ensure all the bytes have been read in
if (offset < bytes.length) {
throw new IOException("Could not completely read file "
+ file.getName());
}
// Close the input stream and return bytes
is.close();
return bytes;
}
/**
* @param args
* : [access token] [text] [pic filename]
*/
public static void main(String[] args) throws Exception {
String access_token = args[0];
Weibo weibo = new Weibo();
weibo.setToken(access_token);
Timeline tl = new Timeline();
//BufferedReader br = new BufferedReader(new FileReader(args[1]));
String text = args[1];
//String line;
//while ((line = br.readLine()) != null)
// text += line;
//br.close();
System.out.println(text);
try {
tl.UploadStatus(text, new ImageItem(getBytesFromFile(new File(
args[2]))));
} catch (WeiboException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}