u
This commit is contained in:
parent
ed4cb18c00
commit
4c8bd3dcd2
@ -372,3 +372,7 @@
|
|||||||
* 2015-07-22
|
* 2015-07-22
|
||||||
|
|
||||||
+ **weixin4j-qy**: 创建标签时可以指定ID
|
+ **weixin4j-qy**: 创建标签时可以指定ID
|
||||||
|
|
||||||
|
* 2015-07-23
|
||||||
|
|
||||||
|
+ 微信支付新增授权码查询OPENID接口
|
||||||
@ -26,8 +26,8 @@ import com.foxinmy.weixin4j.http.ContentType;
|
|||||||
public enum MediaType {
|
public enum MediaType {
|
||||||
image(ContentType.IMAGE_JPG), voice(ContentType.AUDIO_MP3), video(
|
image(ContentType.IMAGE_JPG), voice(ContentType.AUDIO_MP3), video(
|
||||||
ContentType.VIDEO_MPEG4), thumb(ContentType.IMAGE_JPG), file(
|
ContentType.VIDEO_MPEG4), thumb(ContentType.IMAGE_JPG), file(
|
||||||
ContentType.APPLICATION_OCTET_STREAM), news(
|
ContentType.MULTIPART_FORM_DATA), news(
|
||||||
ContentType.APPLICATION_OCTET_STREAM);
|
ContentType.MULTIPART_FORM_DATA);
|
||||||
|
|
||||||
MediaType(ContentType contentType) {
|
MediaType(ContentType contentType) {
|
||||||
this.contentType = contentType;
|
this.contentType = contentType;
|
||||||
|
|||||||
@ -1,8 +1,7 @@
|
|||||||
package com.foxinmy.weixin4j.util;
|
package com.foxinmy.weixin4j.util;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -93,16 +92,14 @@ public class FileUtil {
|
|||||||
/**
|
/**
|
||||||
* 获取文件类型
|
* 获取文件类型
|
||||||
*
|
*
|
||||||
* @param file
|
* @param is
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static String getFileType(File file) {
|
public static String getFileType(InputStream is) {
|
||||||
String fileType = "unknown";
|
String fileType = "file";
|
||||||
FileInputStream fis = null;
|
|
||||||
try {
|
try {
|
||||||
fis = new FileInputStream(file);
|
|
||||||
byte[] b = new byte[10];
|
byte[] b = new byte[10];
|
||||||
int t = fis.read(b, 0, b.length);
|
int t = is.read(b, 0, b.length);
|
||||||
if (t > 0) {
|
if (t > 0) {
|
||||||
String fileCode = bytesToHexString(b).toLowerCase();
|
String fileCode = bytesToHexString(b).toLowerCase();
|
||||||
Iterator<String> keyIter = FILE_TYPE_MAP.keySet().iterator();
|
Iterator<String> keyIter = FILE_TYPE_MAP.keySet().iterator();
|
||||||
@ -115,11 +112,11 @@ public class FileUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
;
|
||||||
} finally {
|
} finally {
|
||||||
if (fis != null) {
|
if (is != null) {
|
||||||
try {
|
try {
|
||||||
fis.close();
|
is.close();
|
||||||
} catch (IOException ignore) {
|
} catch (IOException ignore) {
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -125,24 +125,6 @@ public class WeixinProxy {
|
|||||||
return this.tokenHolder;
|
return this.tokenHolder;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 上传媒体文件
|
|
||||||
*
|
|
||||||
* @param file
|
|
||||||
* 媒体对象
|
|
||||||
* @param isMaterial
|
|
||||||
* 是否永久上传
|
|
||||||
* @return 上传到微信服务器返回的媒体标识
|
|
||||||
* @see {@link com.foxinmy.weixin4j.mp.WeixinProxy#uploadMedia(InputStream, MediaType,boolean)}
|
|
||||||
* @see com.com.foxinmy.weixin4j.mp.api.MediaApi
|
|
||||||
* @throws WeixinException
|
|
||||||
* @throws IOException
|
|
||||||
*/
|
|
||||||
public String uploadMedia(File file, boolean isMaterial)
|
|
||||||
throws WeixinException, IOException {
|
|
||||||
return mediaApi.uploadMedia(file, isMaterial);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 上传媒体文件 </br> <font color="red">此接口只包含图片、语音、缩略图、视频(临时)四种媒体类型的上传</font>
|
* 上传媒体文件 </br> <font color="red">此接口只包含图片、语音、缩略图、视频(临时)四种媒体类型的上传</font>
|
||||||
* <p>
|
* <p>
|
||||||
@ -154,8 +136,8 @@ public class WeixinProxy {
|
|||||||
* 媒体数据流
|
* 媒体数据流
|
||||||
* @param mediaType
|
* @param mediaType
|
||||||
* 媒体文件类型:分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb)
|
* 媒体文件类型:分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb)
|
||||||
* @param isMaterial
|
* @param fileName
|
||||||
* 是否永久上传
|
* 文件名
|
||||||
* @return 上传到微信服务器返回的媒体标识
|
* @return 上传到微信服务器返回的媒体标识
|
||||||
* @see <a
|
* @see <a
|
||||||
* href="http://mp.weixin.qq.com/wiki/5/963fc70b80dc75483a271298a76a8d59.html">上传临时素材</a>
|
* href="http://mp.weixin.qq.com/wiki/5/963fc70b80dc75483a271298a76a8d59.html">上传临时素材</a>
|
||||||
@ -165,9 +147,9 @@ public class WeixinProxy {
|
|||||||
* @see com.com.foxinmy.weixin4j.mp.api.MediaApi
|
* @see com.com.foxinmy.weixin4j.mp.api.MediaApi
|
||||||
* @throws WeixinException
|
* @throws WeixinException
|
||||||
*/
|
*/
|
||||||
public String uploadMedia(InputStream is, MediaType mediaType,
|
public String uploadMedia(InputStream is, String fileName,
|
||||||
boolean isMaterial) throws WeixinException {
|
boolean isMaterial) throws WeixinException {
|
||||||
return mediaApi.uploadMedia(is, mediaType, isMaterial);
|
return mediaApi.uploadMedia(is, fileName, isMaterial);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
package com.foxinmy.weixin4j.mp.api;
|
package com.foxinmy.weixin4j.mp.api;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@ -56,38 +56,8 @@ public class MediaApi extends MpApi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 上传媒体文件
|
* 上传媒体文件:图片(image)、语音(voice)、视频(video)和缩略图(thumb) </br> <font
|
||||||
*
|
* color="red">此接口只包含图片、语音、缩略图、视频(临时)四种媒体类型的上传</font>
|
||||||
* @param file
|
|
||||||
* 文件对象
|
|
||||||
* @param isMaterial
|
|
||||||
* 是否永久上传
|
|
||||||
* @return 上传到微信服务器返回的媒体标识
|
|
||||||
* @see {@link com.foxinmy.weixin4j.mp.api.MediaApi#uploadMedia(InputStream, MediaType,boolean)}
|
|
||||||
* @throws WeixinException
|
|
||||||
* @throws IOException
|
|
||||||
*/
|
|
||||||
public String uploadMedia(File file, boolean isMaterial)
|
|
||||||
throws WeixinException, IOException {
|
|
||||||
String mediaTypeKey = IOUtil.getExtension(file.getName());
|
|
||||||
if (StringUtil.isBlank(mediaTypeKey)) {
|
|
||||||
mediaTypeKey = FileUtil.getFileType(file);
|
|
||||||
}
|
|
||||||
MediaType mediaType = null;
|
|
||||||
if ("bmp/png/jpeg/jpg/gif".contains(mediaTypeKey)) {
|
|
||||||
mediaType = MediaType.image;
|
|
||||||
} else if ("mp3/wma/wav/amr".contains(mediaTypeKey)) {
|
|
||||||
mediaType = MediaType.voice;
|
|
||||||
} else if ("rm/rmvb/wmv/avi/mpg/mpeg/mp4".equals(mediaTypeKey)) {
|
|
||||||
mediaType = MediaType.video;
|
|
||||||
} else {
|
|
||||||
throw new WeixinException("cannot handle mediaType:" + mediaTypeKey);
|
|
||||||
}
|
|
||||||
return uploadMedia(new FileInputStream(file), mediaType, isMaterial);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 上传媒体文件 </br> <font color="red">此接口只包含图片、语音、缩略图、视频(临时)四种媒体类型的上传</font>
|
|
||||||
* <p>
|
* <p>
|
||||||
* 正常情况下返回{"type":"TYPE","media_id":"MEDIA_ID","created_at":123456789},
|
* 正常情况下返回{"type":"TYPE","media_id":"MEDIA_ID","created_at":123456789},
|
||||||
* 否则抛出异常.
|
* 否则抛出异常.
|
||||||
@ -95,8 +65,8 @@ public class MediaApi extends MpApi {
|
|||||||
*
|
*
|
||||||
* @param is
|
* @param is
|
||||||
* 媒体数据流
|
* 媒体数据流
|
||||||
* @param mediaType
|
* @param fileName
|
||||||
* 媒体文件类型:分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb)
|
* 文件名
|
||||||
* @param isMaterial
|
* @param isMaterial
|
||||||
* 是否永久上传
|
* 是否永久上传
|
||||||
* @return 上传到微信服务器返回的媒体标识
|
* @return 上传到微信服务器返回的媒体标识
|
||||||
@ -107,8 +77,33 @@ public class MediaApi extends MpApi {
|
|||||||
* @see com.foxinmy.weixin4j.type.MediaType
|
* @see com.foxinmy.weixin4j.type.MediaType
|
||||||
* @throws WeixinException
|
* @throws WeixinException
|
||||||
*/
|
*/
|
||||||
public String uploadMedia(InputStream is, MediaType mediaType,
|
public String uploadMedia(InputStream is, String fileName,
|
||||||
boolean isMaterial) throws WeixinException {
|
boolean isMaterial) throws WeixinException {
|
||||||
|
byte[] content;
|
||||||
|
try {
|
||||||
|
content = IOUtil.toByteArray(is);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new WeixinException(e);
|
||||||
|
}
|
||||||
|
if (StringUtil.isBlank(fileName)) {
|
||||||
|
fileName = ObjectId.get().toHexString();
|
||||||
|
}
|
||||||
|
String suffixName = IOUtil.getExtension(fileName);
|
||||||
|
if (StringUtil.isBlank(suffixName)) {
|
||||||
|
suffixName = FileUtil
|
||||||
|
.getFileType(new ByteArrayInputStream(content));
|
||||||
|
fileName = String.format("%s.%s", fileName, suffixName);
|
||||||
|
}
|
||||||
|
MediaType mediaType = null;
|
||||||
|
if ("bmp/png/jpeg/jpg/gif".contains(suffixName)) {
|
||||||
|
mediaType = MediaType.image;
|
||||||
|
} else if ("mp3/wma/wav/amr".contains(suffixName)) {
|
||||||
|
mediaType = MediaType.voice;
|
||||||
|
} else if ("rm/rmvb/wmv/avi/mpg/mpeg/mp4".equals(suffixName)) {
|
||||||
|
mediaType = MediaType.video;
|
||||||
|
} else {
|
||||||
|
throw new WeixinException("cannot handle mediaType:" + suffixName);
|
||||||
|
}
|
||||||
if (mediaType == MediaType.video && isMaterial) {
|
if (mediaType == MediaType.video && isMaterial) {
|
||||||
throw new WeixinException(
|
throw new WeixinException(
|
||||||
"please invoke uploadMaterialVideo method");
|
"please invoke uploadMaterialVideo method");
|
||||||
@ -118,21 +113,20 @@ public class MediaApi extends MpApi {
|
|||||||
try {
|
try {
|
||||||
if (isMaterial) {
|
if (isMaterial) {
|
||||||
String material_media_upload_uri = getRequestUri("material_media_upload_uri");
|
String material_media_upload_uri = getRequestUri("material_media_upload_uri");
|
||||||
response = weixinClient.post(
|
response = weixinClient
|
||||||
String.format(material_media_upload_uri,
|
.post(String.format(material_media_upload_uri,
|
||||||
token.getAccessToken()),
|
token.getAccessToken()), new FormBodyPart(
|
||||||
new FormBodyPart("media", new InputStreamBody(is,
|
"media", new InputStreamBody(is, mediaType
|
||||||
mediaType.getContentType().getMimeType(),
|
.getContentType().getMimeType(),
|
||||||
ObjectId.get().toHexString())),
|
fileName)), new FormBodyPart("type",
|
||||||
new FormBodyPart("type", new StringBody(mediaType
|
new StringBody(mediaType.name(), Consts.UTF_8)));
|
||||||
.name(), Consts.UTF_8)));
|
|
||||||
} else {
|
} else {
|
||||||
String media_upload_uri = getRequestUri("media_upload_uri");
|
String media_upload_uri = getRequestUri("media_upload_uri");
|
||||||
response = weixinClient.post(String.format(media_upload_uri,
|
response = weixinClient.post(String.format(media_upload_uri,
|
||||||
token.getAccessToken(), mediaType.name()),
|
token.getAccessToken(), mediaType.name()),
|
||||||
new FormBodyPart("media", new InputStreamBody(is,
|
new FormBodyPart("media", new InputStreamBody(is,
|
||||||
mediaType.getContentType().getMimeType(),
|
mediaType.getContentType().getMimeType(),
|
||||||
ObjectId.get().toHexString())));
|
fileName)));
|
||||||
}
|
}
|
||||||
} catch (UnsupportedEncodingException e) {
|
} catch (UnsupportedEncodingException e) {
|
||||||
throw new WeixinException(e);
|
throw new WeixinException(e);
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
package com.foxinmy.weixin4j.mp.test;
|
package com.foxinmy.weixin4j.mp.test;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -40,8 +41,9 @@ public class MassTest extends TokenTest {
|
|||||||
@Test
|
@Test
|
||||||
public void uploadArticle() throws IOException, WeixinException {
|
public void uploadArticle() throws IOException, WeixinException {
|
||||||
List<MpArticle> articles = new ArrayList<MpArticle>();
|
List<MpArticle> articles = new ArrayList<MpArticle>();
|
||||||
String thumbMediaId = mediaApi.uploadMedia(new File("/tmp/test.jpg"),
|
File file = new File("/tmp/test.jpg");
|
||||||
false);
|
String thumbMediaId = mediaApi.uploadMedia(new FileInputStream(file),
|
||||||
|
file.getName(), false);
|
||||||
articles.add(new MpArticle(thumbMediaId, "title", "content"));
|
articles.add(new MpArticle(thumbMediaId, "title", "content"));
|
||||||
massApi.uploadArticle(articles);
|
massApi.uploadArticle(articles);
|
||||||
}
|
}
|
||||||
@ -69,8 +71,9 @@ public class MassTest extends TokenTest {
|
|||||||
@Test
|
@Test
|
||||||
public void massArticleByGroup() throws IOException, WeixinException {
|
public void massArticleByGroup() throws IOException, WeixinException {
|
||||||
List<MpArticle> articles = new ArrayList<MpArticle>();
|
List<MpArticle> articles = new ArrayList<MpArticle>();
|
||||||
String thumbMediaId = mediaApi.uploadMedia(new File("/tmp/test.jpg"),
|
File file = new File("/tmp/test.jpg");
|
||||||
false);
|
String thumbMediaId = mediaApi.uploadMedia(new FileInputStream(file),
|
||||||
|
file.getName(), false);
|
||||||
articles.add(new MpArticle(thumbMediaId, "title", "content"));
|
articles.add(new MpArticle(thumbMediaId, "title", "content"));
|
||||||
String massId = massApi.massArticleByGroupId(articles, 0);
|
String massId = massApi.massArticleByGroupId(articles, 0);
|
||||||
Assert.assertTrue(massId != null);
|
Assert.assertTrue(massId != null);
|
||||||
@ -79,8 +82,9 @@ public class MassTest extends TokenTest {
|
|||||||
@Test
|
@Test
|
||||||
public void massArticleByOpenIds() throws IOException, WeixinException {
|
public void massArticleByOpenIds() throws IOException, WeixinException {
|
||||||
List<MpArticle> articles = new ArrayList<MpArticle>();
|
List<MpArticle> articles = new ArrayList<MpArticle>();
|
||||||
String thumbMediaId = mediaApi.uploadMedia(new File("/tmp/test.jpg"),
|
File file = new File("/tmp/test.jpg");
|
||||||
false);
|
String thumbMediaId = mediaApi.uploadMedia(new FileInputStream(file),
|
||||||
|
file.getName(), false);
|
||||||
articles.add(new MpArticle(thumbMediaId, "title", "content"));
|
articles.add(new MpArticle(thumbMediaId, "title", "content"));
|
||||||
String massId = massApi.massArticleByOpenIds(articles,
|
String massId = massApi.massArticleByOpenIds(articles,
|
||||||
"owGBft_vbBbOaQOmpEUE4xDLeRSU");
|
"owGBft_vbBbOaQOmpEUE4xDLeRSU");
|
||||||
|
|||||||
@ -40,7 +40,8 @@ public class MediaTest extends TokenTest {
|
|||||||
@Test
|
@Test
|
||||||
public void upload1() throws IOException, WeixinException {
|
public void upload1() throws IOException, WeixinException {
|
||||||
File file = new File("/Users/jy/Downloads/weixin4j.png");
|
File file = new File("/Users/jy/Downloads/weixin4j.png");
|
||||||
String mediaId = mediaApi.uploadMedia(file, false);
|
String mediaId = mediaApi.uploadMedia(new FileInputStream(file),
|
||||||
|
file.getName(), false);
|
||||||
// 1Vgd1R5DdznSc3rPxd-sNZ3pLt54cejhJ5ItuNcCgrqoQArNANWy5oxso_r9KNlE
|
// 1Vgd1R5DdznSc3rPxd-sNZ3pLt54cejhJ5ItuNcCgrqoQArNANWy5oxso_r9KNlE
|
||||||
Assert.assertNotNull(mediaId);
|
Assert.assertNotNull(mediaId);
|
||||||
System.err.println(mediaId);
|
System.err.println(mediaId);
|
||||||
@ -56,7 +57,8 @@ public class MediaTest extends TokenTest {
|
|||||||
@Test
|
@Test
|
||||||
public void upload2() throws IOException, WeixinException {
|
public void upload2() throws IOException, WeixinException {
|
||||||
File file = new File("/Users/jy/Downloads/test.jpg");
|
File file = new File("/Users/jy/Downloads/test.jpg");
|
||||||
String mediaId = mediaApi.uploadMedia(file, true);
|
String mediaId = mediaApi.uploadMedia(new FileInputStream(file),
|
||||||
|
file.getName(), true);
|
||||||
// 8790403529
|
// 8790403529
|
||||||
Assert.assertNotNull(mediaId);
|
Assert.assertNotNull(mediaId);
|
||||||
System.err.println(mediaId);
|
System.err.println(mediaId);
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
package com.foxinmy.weixin4j.mp.test;
|
package com.foxinmy.weixin4j.mp.test;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
@ -89,7 +90,9 @@ public class NotifyTest extends TokenTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void send2() throws WeixinException, IOException {
|
public void send2() throws WeixinException, IOException {
|
||||||
String mediaId = mediaApi.uploadMedia(new File("/tmp/test.jpg"), false);
|
File file = new File("/tmp/test.jpg");
|
||||||
|
String mediaId = mediaApi.uploadMedia(new FileInputStream(file),
|
||||||
|
file.getName(), false);
|
||||||
NotifyMessage imageNotify = new NotifyMessage(
|
NotifyMessage imageNotify = new NotifyMessage(
|
||||||
"owGBft_vbBbOaQOmpEUE4xDLeRSU", new Image(mediaId));
|
"owGBft_vbBbOaQOmpEUE4xDLeRSU", new Image(mediaId));
|
||||||
JsonResult result = notifyApi.sendNotify(imageNotify);
|
JsonResult result = notifyApi.sendNotify(imageNotify);
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
package com.foxinmy.weixin4j.qy;
|
package com.foxinmy.weixin4j.qy;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -205,24 +204,6 @@ public class WeixinProxy {
|
|||||||
return menuApi.deleteMenu(agentid);
|
return menuApi.deleteMenu(agentid);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 上传媒体文件
|
|
||||||
*
|
|
||||||
* @param agentid
|
|
||||||
* 企业应用ID(<font color="red">大于0时视为上传永久媒体文件</font>)
|
|
||||||
* @param file
|
|
||||||
* 文件对象
|
|
||||||
* @return 上传到微信服务器返回的媒体标识
|
|
||||||
* @see com.foxinmy.weixin4j.qy.api.MediaApi
|
|
||||||
* @see {@link com.foxinmy.weixin4j.qy.api.MediaApi#uploadMedia(int,InputStream, MediaType)}
|
|
||||||
* @throws WeixinException
|
|
||||||
* @throws IOException
|
|
||||||
*/
|
|
||||||
public String uploadMedia(int agentid, File file) throws WeixinException,
|
|
||||||
IOException {
|
|
||||||
return mediaApi.uploadMedia(agentid, file);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 上传媒体文件
|
* 上传媒体文件
|
||||||
* <p>
|
* <p>
|
||||||
@ -234,8 +215,8 @@ public class WeixinProxy {
|
|||||||
* 企业应用ID(<font color="red">大于0时视为上传永久媒体文件</font>)
|
* 企业应用ID(<font color="red">大于0时视为上传永久媒体文件</font>)
|
||||||
* @param is
|
* @param is
|
||||||
* 媒体数据流
|
* 媒体数据流
|
||||||
* @param mediaType
|
* @param fileName
|
||||||
* 媒体类型
|
* 文件名
|
||||||
* @return 上传到微信服务器返回的媒体标识
|
* @return 上传到微信服务器返回的媒体标识
|
||||||
* @see com.foxinmy.weixin4j.qy.api.MediaApi
|
* @see com.foxinmy.weixin4j.qy.api.MediaApi
|
||||||
* @see <a
|
* @see <a
|
||||||
@ -244,9 +225,9 @@ public class WeixinProxy {
|
|||||||
* href="http://http://qydev.weixin.qq.com/wiki/index.php?title=%E4%B8%8A%E4%BC%A0%E6%B0%B8%E4%B9%85%E7%B4%A0%E6%9D%90">上传永久素材文件说明</a>
|
* href="http://http://qydev.weixin.qq.com/wiki/index.php?title=%E4%B8%8A%E4%BC%A0%E6%B0%B8%E4%B9%85%E7%B4%A0%E6%9D%90">上传永久素材文件说明</a>
|
||||||
* @throws WeixinException
|
* @throws WeixinException
|
||||||
*/
|
*/
|
||||||
public String uploadMedia(int agentid, InputStream is, MediaType mediaType)
|
public String uploadMedia(int agentid, InputStream is, String fileName)
|
||||||
throws WeixinException {
|
throws WeixinException {
|
||||||
return mediaApi.uploadMedia(agentid, is, mediaType);
|
return mediaApi.uploadMedia(agentid, is, fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -2,7 +2,6 @@ package com.foxinmy.weixin4j.qy.api;
|
|||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@ -60,37 +59,6 @@ public class MediaApi extends QyApi {
|
|||||||
this.tokenHolder = tokenHolder;
|
this.tokenHolder = tokenHolder;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 上传媒体文件
|
|
||||||
*
|
|
||||||
* @param agentid
|
|
||||||
* 企业应用ID(<font color="red">大于0时视为上传永久媒体文件</font>)
|
|
||||||
* @param file
|
|
||||||
* 文件对象
|
|
||||||
* @return 上传到微信服务器返回的媒体标识
|
|
||||||
* @see {@link com.foxinmy.weixin4j.qy.api.MediaApi#uploadMedia(int,InputStream, MediaType)}
|
|
||||||
* @throws WeixinException
|
|
||||||
* @throws IOException
|
|
||||||
*/
|
|
||||||
public String uploadMedia(int agentid, File file) throws WeixinException,
|
|
||||||
IOException {
|
|
||||||
String mediaTypeKey = IOUtil.getExtension(file.getName());
|
|
||||||
if (StringUtil.isBlank(mediaTypeKey)) {
|
|
||||||
mediaTypeKey = FileUtil.getFileType(file);
|
|
||||||
}
|
|
||||||
MediaType mediaType = null;
|
|
||||||
if ("bmp/png/jpeg/jpg/gif".contains(mediaTypeKey)) {
|
|
||||||
mediaType = MediaType.image;
|
|
||||||
} else if ("mp3/wma/wav/amr".contains(mediaTypeKey)) {
|
|
||||||
mediaType = MediaType.voice;
|
|
||||||
} else if ("rm/rmvb/wmv/avi/mpg/mpeg/mp4".equals(mediaTypeKey)) {
|
|
||||||
mediaType = MediaType.video;
|
|
||||||
} else {
|
|
||||||
mediaType = MediaType.file;
|
|
||||||
}
|
|
||||||
return uploadMedia(agentid, new FileInputStream(file), mediaType);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 上传媒体文件
|
* 上传媒体文件
|
||||||
* <p>
|
* <p>
|
||||||
@ -102,8 +70,8 @@ public class MediaApi extends QyApi {
|
|||||||
* 企业应用ID(<font color="red">大于0时视为上传永久媒体文件</font>)
|
* 企业应用ID(<font color="red">大于0时视为上传永久媒体文件</font>)
|
||||||
* @param is
|
* @param is
|
||||||
* 媒体数据流
|
* 媒体数据流
|
||||||
* @param mediaType
|
* @param fileName
|
||||||
* 媒体类型
|
* 文件名
|
||||||
* @return 上传到微信服务器返回的媒体标识
|
* @return 上传到微信服务器返回的媒体标识
|
||||||
* @see <a
|
* @see <a
|
||||||
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E4%B8%8A%E4%BC%A0%E4%B8%B4%E6%97%B6%E7%B4%A0%E6%9D%90%E6%96%87%E4%BB%B6">上传临时素材文件说明</a>
|
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E4%B8%8A%E4%BC%A0%E4%B8%B4%E6%97%B6%E7%B4%A0%E6%9D%90%E6%96%87%E4%BB%B6">上传临时素材文件说明</a>
|
||||||
@ -111,17 +79,34 @@ public class MediaApi extends QyApi {
|
|||||||
* href="http://http://qydev.weixin.qq.com/wiki/index.php?title=%E4%B8%8A%E4%BC%A0%E6%B0%B8%E4%B9%85%E7%B4%A0%E6%9D%90">上传永久素材文件说明</a>
|
* href="http://http://qydev.weixin.qq.com/wiki/index.php?title=%E4%B8%8A%E4%BC%A0%E6%B0%B8%E4%B9%85%E7%B4%A0%E6%9D%90">上传永久素材文件说明</a>
|
||||||
* @throws WeixinException
|
* @throws WeixinException
|
||||||
*/
|
*/
|
||||||
public String uploadMedia(int agentid, InputStream is, MediaType mediaType)
|
public String uploadMedia(int agentid, InputStream is, String fileName)
|
||||||
throws WeixinException {
|
throws WeixinException {
|
||||||
Token token = tokenHolder.getToken();
|
byte[] content;
|
||||||
if (mediaType == null) {
|
try {
|
||||||
mediaType = MediaType.file;
|
content = IOUtil.toByteArray(is);
|
||||||
} else if (mediaType == MediaType.thumb) {
|
} catch (IOException e) {
|
||||||
mediaType = MediaType.image;
|
throw new WeixinException(e);
|
||||||
} else if (mediaType == MediaType.news) {
|
|
||||||
throw new WeixinException(
|
|
||||||
"please invoke uploadMaterialArticle method");
|
|
||||||
}
|
}
|
||||||
|
if (StringUtil.isBlank(fileName)) {
|
||||||
|
fileName = ObjectId.get().toHexString();
|
||||||
|
}
|
||||||
|
String suffixName = IOUtil.getExtension(fileName);
|
||||||
|
if (StringUtil.isBlank(suffixName)) {
|
||||||
|
suffixName = FileUtil
|
||||||
|
.getFileType(new ByteArrayInputStream(content));
|
||||||
|
fileName = String.format("%s.%s", fileName, suffixName);
|
||||||
|
}
|
||||||
|
MediaType mediaType = null;
|
||||||
|
if ("bmp/png/jpeg/jpg/gif".contains(suffixName)) {
|
||||||
|
mediaType = MediaType.image;
|
||||||
|
} else if ("mp3/wma/wav/amr".contains(suffixName)) {
|
||||||
|
mediaType = MediaType.voice;
|
||||||
|
} else if ("rm/rmvb/wmv/avi/mpg/mpeg/mp4".equals(suffixName)) {
|
||||||
|
mediaType = MediaType.video;
|
||||||
|
} else {
|
||||||
|
mediaType = MediaType.file;
|
||||||
|
}
|
||||||
|
Token token = tokenHolder.getToken();
|
||||||
try {
|
try {
|
||||||
WeixinResponse response = null;
|
WeixinResponse response = null;
|
||||||
if (agentid > 0) {
|
if (agentid > 0) {
|
||||||
@ -129,15 +114,17 @@ public class MediaApi extends QyApi {
|
|||||||
response = weixinClient.post(String.format(
|
response = weixinClient.post(String.format(
|
||||||
material_media_upload_uri, token.getAccessToken(),
|
material_media_upload_uri, token.getAccessToken(),
|
||||||
mediaType.name(), agentid), new FormBodyPart("media",
|
mediaType.name(), agentid), new FormBodyPart("media",
|
||||||
new InputStreamBody(is, mediaType.getContentType()
|
new InputStreamBody(new ByteArrayInputStream(content),
|
||||||
.getMimeType(), ObjectId.get().toHexString())));
|
mediaType.getContentType().getMimeType(),
|
||||||
|
fileName)));
|
||||||
} else {
|
} else {
|
||||||
String file_upload_uri = getRequestUri("file_upload_uri");
|
String file_upload_uri = getRequestUri("file_upload_uri");
|
||||||
response = weixinClient.post(String.format(file_upload_uri,
|
response = weixinClient.post(String.format(file_upload_uri,
|
||||||
token.getAccessToken(), mediaType.name()),
|
token.getAccessToken(), mediaType.name()),
|
||||||
new FormBodyPart("media", new InputStreamBody(is,
|
new FormBodyPart("media", new InputStreamBody(
|
||||||
mediaType.getContentType().getMimeType(),
|
new ByteArrayInputStream(content), mediaType
|
||||||
ObjectId.get().toHexString())));
|
.getContentType().getMimeType(),
|
||||||
|
fileName)));
|
||||||
}
|
}
|
||||||
return response.getAsJson().getString("media_id");
|
return response.getAsJson().getString("media_id");
|
||||||
} finally {
|
} finally {
|
||||||
@ -468,7 +455,7 @@ public class MediaApi extends QyApi {
|
|||||||
writer.write("\r\n");
|
writer.write("\r\n");
|
||||||
}
|
}
|
||||||
return uploadMedia(0, new ByteArrayInputStream(writer.getBuffer()
|
return uploadMedia(0, new ByteArrayInputStream(writer.getBuffer()
|
||||||
.toString().getBytes(Consts.UTF_8)), MediaType.file);
|
.toString().getBytes(Consts.UTF_8)), batchName);
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
writer.close();
|
writer.close();
|
||||||
|
|||||||
@ -16,7 +16,6 @@ import com.foxinmy.weixin4j.qy.model.User;
|
|||||||
import com.foxinmy.weixin4j.qy.type.InviteType;
|
import com.foxinmy.weixin4j.qy.type.InviteType;
|
||||||
import com.foxinmy.weixin4j.qy.type.UserStatus;
|
import com.foxinmy.weixin4j.qy.type.UserStatus;
|
||||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||||
import com.foxinmy.weixin4j.type.MediaType;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 成员API
|
* 成员API
|
||||||
@ -118,8 +117,7 @@ public class UserApi extends QyApi {
|
|||||||
obj.put("extattr", attrs);
|
obj.put("extattr", attrs);
|
||||||
}
|
}
|
||||||
if (avatar != null) {
|
if (avatar != null) {
|
||||||
obj.put("avatar_mediaid",
|
obj.put("avatar_mediaid", mediaApi.uploadMedia(0, avatar, null));
|
||||||
mediaApi.uploadMedia(0, avatar, MediaType.image));
|
|
||||||
}
|
}
|
||||||
Token token = tokenHolder.getToken();
|
Token token = tokenHolder.getToken();
|
||||||
WeixinResponse response = weixinClient.post(
|
WeixinResponse response = weixinClient.post(
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
package com.foxinmy.weixin4j.qy.test;
|
package com.foxinmy.weixin4j.qy.test;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
@ -31,8 +32,9 @@ public class MediaTest extends TokenTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void upload() throws IOException, WeixinException {
|
public void upload() throws IOException, WeixinException {
|
||||||
File file = new File("//Users/jy/Downloads/import_file.csv");
|
File file = new File("/Users/jy/Downloads/uu-logo.png");
|
||||||
String mediaId = mediaApi.uploadMedia(3, file);
|
String mediaId = mediaApi.uploadMedia(0, new FileInputStream(file),
|
||||||
|
file.getName());
|
||||||
// 1-1gpykXsR8bhNvO13-ZvskptCBxQF1UE535jFdCF63N2inGRAqEb-psF6eppjIIl
|
// 1-1gpykXsR8bhNvO13-ZvskptCBxQF1UE535jFdCF63N2inGRAqEb-psF6eppjIIl
|
||||||
// 1CF6sBgWWFGY9s4JCEet5ASszsTuyHpeN1f2LWXADveqBlKoxSgb3cO401NEM7dNY
|
// 1CF6sBgWWFGY9s4JCEet5ASszsTuyHpeN1f2LWXADveqBlKoxSgb3cO401NEM7dNY
|
||||||
Assert.assertNotNull(mediaId);
|
Assert.assertNotNull(mediaId);
|
||||||
@ -42,7 +44,8 @@ public class MediaTest extends TokenTest {
|
|||||||
@Test
|
@Test
|
||||||
public void download() throws WeixinException, IOException {
|
public void download() throws WeixinException, IOException {
|
||||||
File file = mediaApi
|
File file = mediaApi
|
||||||
.downloadMediaFile(3,
|
.downloadMediaFile(
|
||||||
|
3,
|
||||||
"272LZlRmz1h7V2lcsvouCxwbJ_Dh-rgdDecX_26f_HDzJSZiSZjBeqeSYI1r9Ad9q66iWTGmRDUFgWOvz_fGVGi1BRZ4wjtkhPe2XcK-oomk");
|
"272LZlRmz1h7V2lcsvouCxwbJ_Dh-rgdDecX_26f_HDzJSZiSZjBeqeSYI1r9Ad9q66iWTGmRDUFgWOvz_fGVGi1BRZ4wjtkhPe2XcK-oomk");
|
||||||
Assert.assertTrue(file.exists());
|
Assert.assertTrue(file.exists());
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user