下面是我上传视频素材的代码,但是返回的是{"errcode":-1,"errmsg":"system error"}
参数:
requestUrl = “http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=ViuWPniaMkeerWvEO7X8GC8tNGCxeYs-jurzRonQqrzSLUHX1U4rR3yHsD8Ravr-70SQW0Xi1XhdehAzINECP8Il6d9z1ySt6pFn9qgjji4&type=video”
requestMethod = “POST”
mediaUrlStr = “http://localhost:8080/uploadFile/weChatInfoRelease/mp4/2015/5/12/153420435992.mp4”
public static JSONObject httpsRequestForUpload(String requestUrl,
String requestMethod, String mediaUrlStr) {
JSONObject jsonObject = null;
StringBuffer buffer = new StringBuffer();
try {
URL url = new URL(requestUrl);
HttpURLConnection httpUrlConn = (HttpURLConnection) url
.openConnection();
httpUrlConn.setDoOutput(true);
httpUrlConn.setDoInput(true);
/** 设置请求方式(GET/POST) */
httpUrlConn.setRequestMethod(requestMethod);
httpUrlConn.setRequestProperty("Content-Type",
"multipart/form-data;boundary=----" + boundary);
OutputStream outputStream = httpUrlConn.getOutputStream();
URL mediaUrl = new URL(mediaUrlStr);
HttpURLConnection mediaConn = (HttpURLConnection) mediaUrl
.openConnection();
mediaConn.setReadTimeout(5000);
mediaConn.setDoOutput(true);
mediaConn.setRequestMethod(Constants.GET);
// 请求头中获取内容类型
String fileExt = mediaUrlStr
.substring(mediaUrlStr.lastIndexOf("."));
String contentType = getContentType(fileExt);
String fileName = mediaUrlStr.substring(
mediaUrlStr.lastIndexOf("/") + 1,
mediaUrlStr.lastIndexOf("."));
// 请求开始
outputStream.write(("------" + boundary + "\r\n").getBytes());
outputStream
.write(String
.format("Content-Disposition:form-data;name=\"media\";filename=\"%s%s\"\r\n",
fileName, fileExt).getBytes());
outputStream.write(String.format("Content-Type:%s\r\n\r\n",
contentType).getBytes());
// 获取媒体文件的输入流(读取文件)
BufferedInputStream bis = new BufferedInputStream(
mediaConn.getInputStream());
byte[] buf = new byte[8096];
int size = 0;
while ((size = bis.read(buf)) != -1) {
outputStream.write(buf, 0, size);
}
// 请求体结束
outputStream.write(("\r\n------" + boundary + "--\r\n").getBytes());
outputStream.close();
bis.close();
mediaConn.disconnect();
/** 将返回的输入流转换成字符串 */
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(
inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(
inputStreamReader);
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
/** 释放资源 */
inputStream.close();
inputStream = null;
httpUrlConn.disconnect();
jsonObject = JSONObject.fromObject(buffer.toString());
} catch (ConnectException ce) {
logger.error("Weixin server connection timed out.");
} catch (Exception e) {
logger.error("https request error:" + e);
}
return jsonObject;
}
求高手指教