新浪微博上传图片---Android代码示例
在做新浪微博客户端上传图片时,API的解释一直很不给力,由于上传图片必须模拟HttpClient的行为模式,导致移动客户端的上传图片模块开发比较棘手。无奈只能旁征博引,参考其他资料,以下代码在Android平台下测试已经通过。
public class HttpFormImage {
private final static String CN = "HttpFormImage";
private final static String BOUNDARY = "sdg8sf8vdsd8";
private final static String MP_BOUNDARY = "--" + BOUNDARY;
private final static String END_MP_BOUNDARY = "--" + BOUNDARY + "--";
public boolean sendMsg(String url, String access_token, String text,
File image,String imageType) {
ByteArrayOutputStream bos = null;
byte[] data = null;
try {
HttpPost post = new HttpPost(url);
post.setHeader("Content-Type", "multipart/form-data; boundary="
+ BOUNDARY);
bos = new ByteArrayOutputStream();
paramToUpload(bos, "access_token", access_token);
paramToUpload(bos, "status", text);
imageContentToUpload(bos, image,imageType);
data = bos.toByteArray();
bos.close();
ByteArrayEntity formEntity = new ByteArrayEntity(data);
post.setEntity(formEntity);
HttpResponse response = new DefaultHttpClient().execute(post);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
Log.i(CN + "_sendMsg", statusCode + "");
return true;
} else if (statusCode > Const.ERR_ERR) {
Log.e(CN + "_sendMsg", Weto.exp2str(statusCode) + "");
return false;
} else {
Log.e(CN + "_sendMsg", statusCode + "");
return false;
}
} catch (Exception e) {
Log.e(CN + "_sendMsg", Const.EXP_IO + "", e);
}
return false;
}
/***
*
* @param bos
* outputstream ,write to network
* @param key
* parameter name
* @param value
* parameter value
*/
private static void paramToUpload(OutputStream bos, String key, String value) {
StringBuilder temp = new StringBuilder(10);
temp.setLength(0);
temp.append(MP_BOUNDARY).append("rn");
temp.append("content-disposition: form-data; name="").append(key)
.append(""rnrn");
temp.append(value).append("rn");
byte[] res = temp.toString().getBytes();
try {
bos.write(res);
} catch (IOException e) {
Log.e(CN + "_paramToUpload", Const.EXP_IO + "", e);
}
}
/***
*
* @param out
* outputStream,write to server
* @param file
* File
* @param imageType
* image type==image/jpg,image/png, image/gif
*/
private static void imageContentToUpload(OutputStream out, File file,
String imageType) {
StringBuilder temp = new StringBuilder();
temp.append(MP_BOUNDARY).append("rn");
temp.append("Content-Disposition: form-data; name="pic"; filename="")
.append(file.getName()).append(""rn");
temp.append("Content-Type: ").append(imageType).append("rnrn");
byte[] res = temp.toString().getBytes();
FileInputStream input = null;
try {
out.write(res);
input = new FileInputStream(file.getAbsoluteFile());
byte[] buffer = new byte[1024 * 50];
while (true) {
int count = input.read(buffer);
if (count == -1) {
break;
}
out.write(buffer, 0, count);
}
out.write("rn".getBytes());
out.write(("rn" + END_MP_BOUNDARY).getBytes());
} catch (IOException e) {
Log.e(CN + "_imageContentToUpload", Const.EXP_IO + "", e);
} finally {
if (null != input) {
try {
input.close();
} catch (IOException e) {
Log.e(CN + "_imageContentToUpload", Const.EXP_IO + "", e);
}
}
}
}
}