This commit is contained in:
jinyu 2016-02-20 16:15:24 +08:00
parent f97a0de327
commit 54c2ab0ed1
3 changed files with 19 additions and 12 deletions

View File

@ -320,6 +320,8 @@ public class WeixinProxy {
* *
* @param is * @param is
* 大小不超过1M且格式为MP4的视频文件 * 大小不超过1M且格式为MP4的视频文件
* @param fileName
* 文件名 为空时将自动生成
* @param title * @param title
* 视频标题 * 视频标题
* @param introduction * @param introduction
@ -330,9 +332,9 @@ public class WeixinProxy {
* @see com.foxinmy.weixin4j.mp.api.MediaApi * @see com.foxinmy.weixin4j.mp.api.MediaApi
* @throws WeixinException * @throws WeixinException
*/ */
public String uploadMaterialVideo(InputStream is, String title, public String uploadMaterialVideo(InputStream is, String fileName,
String introduction) throws WeixinException { String title, String introduction) throws WeixinException {
return mediaApi.uploadMaterialVideo(is, title, introduction); return mediaApi.uploadMaterialVideo(is, fileName, title, introduction);
} }
/** /**

View File

@ -400,17 +400,25 @@ public class MediaApi extends MpApi {
* *
* @param is * @param is
* 大小不超过1M且格式为MP4的视频文件 * 大小不超过1M且格式为MP4的视频文件
* @param fileName
* 文件名 为空时将自动生成
* @param title * @param title
* 视频标题 * 视频标题
* @param introduction * @param introduction
* 视频描述 * 视频描述 可以为空
* @return 上传到微信服务器返回的媒体标识 * @return 上传到微信服务器返回的媒体标识
* @see <a * @see <a
* href="http://mp.weixin.qq.com/wiki/14/7e6c03263063f4813141c3e17dd4350a.html">上传永久媒体素材</a> * href="http://mp.weixin.qq.com/wiki/14/7e6c03263063f4813141c3e17dd4350a.html">上传永久媒体素材</a>
* @throws WeixinException * @throws WeixinException
*/ */
public String uploadMaterialVideo(InputStream is, String title, public String uploadMaterialVideo(InputStream is, String fileName,
String introduction) throws WeixinException { String title, String introduction) throws WeixinException {
if (StringUtil.isBlank(fileName)) {
fileName = ObjectId.get().toHexString();
}
if (StringUtil.isBlank(FileUtil.getFileExtension(fileName))) {
fileName = String.format("%s.mp4", fileName);
}
String material_media_upload_uri = getRequestUri("material_media_upload_uri"); String material_media_upload_uri = getRequestUri("material_media_upload_uri");
Token token = tokenHolder.getToken(); Token token = tokenHolder.getToken();
try { try {
@ -421,10 +429,7 @@ public class MediaApi extends MpApi {
String.format(material_media_upload_uri, String.format(material_media_upload_uri,
token.getAccessToken()), token.getAccessToken()),
new FormBodyPart("media", new InputStreamBody(is, new FormBodyPart("media", new InputStreamBody(is,
MediaType.video.getContentType().getMimeType(), ContentType.VIDEO_MPEG4.getMimeType(), fileName)),
ObjectId.get().toHexString())),
new FormBodyPart("type", new StringBody(MediaType.video
.name(), Consts.UTF_8)),
new FormBodyPart("description", new StringBody(description new FormBodyPart("description", new StringBody(description
.toJSONString(), Consts.UTF_8))); .toJSONString(), Consts.UTF_8)));
return response.getAsJson().getString("media_id"); return response.getAsJson().getString("media_id");

View File

@ -74,9 +74,9 @@ public class MediaTest extends TokenTest {
@Test @Test
public void uploadMaterialVideo() throws IOException, WeixinException { public void uploadMaterialVideo() throws IOException, WeixinException {
File file = new File("/Users/jy/Downloads/test.jpg"); File file = new File("/Users/jy/Downloads/test.mp4");
String mediaId = mediaApi.uploadMaterialVideo( String mediaId = mediaApi.uploadMaterialVideo(
new FileInputStream(file), "title", "introduction"); new FileInputStream(file), "filename", "title", "introduction");
// Sy1KOLsi4ri3kB3TYUuculVelcW2I7W6BrfGwkGvSW8beTCAarxuGQLjuNJChJr8 // Sy1KOLsi4ri3kB3TYUuculVelcW2I7W6BrfGwkGvSW8beTCAarxuGQLjuNJChJr8
Assert.assertNotNull(mediaId); Assert.assertNotNull(mediaId);
System.err.println(mediaId); System.err.println(mediaId);