u
This commit is contained in:
parent
ed4cb18c00
commit
4c8bd3dcd2
@ -371,4 +371,8 @@
|
||||
|
||||
* 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 {
|
||||
image(ContentType.IMAGE_JPG), voice(ContentType.AUDIO_MP3), video(
|
||||
ContentType.VIDEO_MPEG4), thumb(ContentType.IMAGE_JPG), file(
|
||||
ContentType.APPLICATION_OCTET_STREAM), news(
|
||||
ContentType.APPLICATION_OCTET_STREAM);
|
||||
ContentType.MULTIPART_FORM_DATA), news(
|
||||
ContentType.MULTIPART_FORM_DATA);
|
||||
|
||||
MediaType(ContentType contentType) {
|
||||
this.contentType = contentType;
|
||||
|
||||
@ -1,8 +1,7 @@
|
||||
package com.foxinmy.weixin4j.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
@ -93,16 +92,14 @@ public class FileUtil {
|
||||
/**
|
||||
* 获取文件类型
|
||||
*
|
||||
* @param file
|
||||
* @param is
|
||||
* @return
|
||||
*/
|
||||
public static String getFileType(File file) {
|
||||
String fileType = "unknown";
|
||||
FileInputStream fis = null;
|
||||
public static String getFileType(InputStream is) {
|
||||
String fileType = "file";
|
||||
try {
|
||||
fis = new FileInputStream(file);
|
||||
byte[] b = new byte[10];
|
||||
int t = fis.read(b, 0, b.length);
|
||||
int t = is.read(b, 0, b.length);
|
||||
if (t > 0) {
|
||||
String fileCode = bytesToHexString(b).toLowerCase();
|
||||
Iterator<String> keyIter = FILE_TYPE_MAP.keySet().iterator();
|
||||
@ -115,11 +112,11 @@ public class FileUtil {
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
;
|
||||
} finally {
|
||||
if (fis != null) {
|
||||
if (is != null) {
|
||||
try {
|
||||
fis.close();
|
||||
is.close();
|
||||
} catch (IOException ignore) {
|
||||
;
|
||||
}
|
||||
|
||||
@ -125,24 +125,6 @@ public class WeixinProxy {
|
||||
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>
|
||||
* <p>
|
||||
@ -154,8 +136,8 @@ public class WeixinProxy {
|
||||
* 媒体数据流
|
||||
* @param mediaType
|
||||
* 媒体文件类型:分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb)
|
||||
* @param isMaterial
|
||||
* 是否永久上传
|
||||
* @param fileName
|
||||
* 文件名
|
||||
* @return 上传到微信服务器返回的媒体标识
|
||||
* @see <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
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public String uploadMedia(InputStream is, MediaType mediaType,
|
||||
public String uploadMedia(InputStream is, String fileName,
|
||||
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;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@ -56,38 +56,8 @@ public class MediaApi extends MpApi {
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传媒体文件
|
||||
*
|
||||
* @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>
|
||||
* 上传媒体文件:图片(image)、语音(voice)、视频(video)和缩略图(thumb) </br> <font
|
||||
* color="red">此接口只包含图片、语音、缩略图、视频(临时)四种媒体类型的上传</font>
|
||||
* <p>
|
||||
* 正常情况下返回{"type":"TYPE","media_id":"MEDIA_ID","created_at":123456789},
|
||||
* 否则抛出异常.
|
||||
@ -95,8 +65,8 @@ public class MediaApi extends MpApi {
|
||||
*
|
||||
* @param is
|
||||
* 媒体数据流
|
||||
* @param mediaType
|
||||
* 媒体文件类型:分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb)
|
||||
* @param fileName
|
||||
* 文件名
|
||||
* @param isMaterial
|
||||
* 是否永久上传
|
||||
* @return 上传到微信服务器返回的媒体标识
|
||||
@ -107,8 +77,33 @@ public class MediaApi extends MpApi {
|
||||
* @see com.foxinmy.weixin4j.type.MediaType
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public String uploadMedia(InputStream is, MediaType mediaType,
|
||||
public String uploadMedia(InputStream is, String fileName,
|
||||
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) {
|
||||
throw new WeixinException(
|
||||
"please invoke uploadMaterialVideo method");
|
||||
@ -118,21 +113,20 @@ public class MediaApi extends MpApi {
|
||||
try {
|
||||
if (isMaterial) {
|
||||
String material_media_upload_uri = getRequestUri("material_media_upload_uri");
|
||||
response = weixinClient.post(
|
||||
String.format(material_media_upload_uri,
|
||||
token.getAccessToken()),
|
||||
new FormBodyPart("media", new InputStreamBody(is,
|
||||
mediaType.getContentType().getMimeType(),
|
||||
ObjectId.get().toHexString())),
|
||||
new FormBodyPart("type", new StringBody(mediaType
|
||||
.name(), Consts.UTF_8)));
|
||||
response = weixinClient
|
||||
.post(String.format(material_media_upload_uri,
|
||||
token.getAccessToken()), new FormBodyPart(
|
||||
"media", new InputStreamBody(is, mediaType
|
||||
.getContentType().getMimeType(),
|
||||
fileName)), new FormBodyPart("type",
|
||||
new StringBody(mediaType.name(), Consts.UTF_8)));
|
||||
} else {
|
||||
String media_upload_uri = getRequestUri("media_upload_uri");
|
||||
response = weixinClient.post(String.format(media_upload_uri,
|
||||
token.getAccessToken(), mediaType.name()),
|
||||
new FormBodyPart("media", new InputStreamBody(is,
|
||||
mediaType.getContentType().getMimeType(),
|
||||
ObjectId.get().toHexString())));
|
||||
fileName)));
|
||||
}
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new WeixinException(e);
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package com.foxinmy.weixin4j.mp.test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@ -40,8 +41,9 @@ public class MassTest extends TokenTest {
|
||||
@Test
|
||||
public void uploadArticle() throws IOException, WeixinException {
|
||||
List<MpArticle> articles = new ArrayList<MpArticle>();
|
||||
String thumbMediaId = mediaApi.uploadMedia(new File("/tmp/test.jpg"),
|
||||
false);
|
||||
File file = new File("/tmp/test.jpg");
|
||||
String thumbMediaId = mediaApi.uploadMedia(new FileInputStream(file),
|
||||
file.getName(), false);
|
||||
articles.add(new MpArticle(thumbMediaId, "title", "content"));
|
||||
massApi.uploadArticle(articles);
|
||||
}
|
||||
@ -69,8 +71,9 @@ public class MassTest extends TokenTest {
|
||||
@Test
|
||||
public void massArticleByGroup() throws IOException, WeixinException {
|
||||
List<MpArticle> articles = new ArrayList<MpArticle>();
|
||||
String thumbMediaId = mediaApi.uploadMedia(new File("/tmp/test.jpg"),
|
||||
false);
|
||||
File file = new File("/tmp/test.jpg");
|
||||
String thumbMediaId = mediaApi.uploadMedia(new FileInputStream(file),
|
||||
file.getName(), false);
|
||||
articles.add(new MpArticle(thumbMediaId, "title", "content"));
|
||||
String massId = massApi.massArticleByGroupId(articles, 0);
|
||||
Assert.assertTrue(massId != null);
|
||||
@ -79,8 +82,9 @@ public class MassTest extends TokenTest {
|
||||
@Test
|
||||
public void massArticleByOpenIds() throws IOException, WeixinException {
|
||||
List<MpArticle> articles = new ArrayList<MpArticle>();
|
||||
String thumbMediaId = mediaApi.uploadMedia(new File("/tmp/test.jpg"),
|
||||
false);
|
||||
File file = new File("/tmp/test.jpg");
|
||||
String thumbMediaId = mediaApi.uploadMedia(new FileInputStream(file),
|
||||
file.getName(), false);
|
||||
articles.add(new MpArticle(thumbMediaId, "title", "content"));
|
||||
String massId = massApi.massArticleByOpenIds(articles,
|
||||
"owGBft_vbBbOaQOmpEUE4xDLeRSU");
|
||||
|
||||
@ -40,7 +40,8 @@ public class MediaTest extends TokenTest {
|
||||
@Test
|
||||
public void upload1() throws IOException, WeixinException {
|
||||
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
|
||||
Assert.assertNotNull(mediaId);
|
||||
System.err.println(mediaId);
|
||||
@ -56,7 +57,8 @@ public class MediaTest extends TokenTest {
|
||||
@Test
|
||||
public void upload2() throws IOException, WeixinException {
|
||||
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
|
||||
Assert.assertNotNull(mediaId);
|
||||
System.err.println(mediaId);
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package com.foxinmy.weixin4j.mp.test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Assert;
|
||||
@ -89,7 +90,9 @@ public class NotifyTest extends TokenTest {
|
||||
|
||||
@Test
|
||||
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(
|
||||
"owGBft_vbBbOaQOmpEUE4xDLeRSU", new Image(mediaId));
|
||||
JsonResult result = notifyApi.sendNotify(imageNotify);
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
package com.foxinmy.weixin4j.qy;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
|
||||
@ -205,24 +204,6 @@ public class WeixinProxy {
|
||||
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>
|
||||
@ -234,8 +215,8 @@ public class WeixinProxy {
|
||||
* 企业应用ID(<font color="red">大于0时视为上传永久媒体文件</font>)
|
||||
* @param is
|
||||
* 媒体数据流
|
||||
* @param mediaType
|
||||
* 媒体类型
|
||||
* @param fileName
|
||||
* 文件名
|
||||
* @return 上传到微信服务器返回的媒体标识
|
||||
* @see com.foxinmy.weixin4j.qy.api.MediaApi
|
||||
* @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>
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public String uploadMedia(int agentid, InputStream is, MediaType mediaType)
|
||||
public String uploadMedia(int agentid, InputStream is, String fileName)
|
||||
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.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@ -60,37 +59,6 @@ public class MediaApi extends QyApi {
|
||||
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>
|
||||
@ -102,8 +70,8 @@ public class MediaApi extends QyApi {
|
||||
* 企业应用ID(<font color="red">大于0时视为上传永久媒体文件</font>)
|
||||
* @param is
|
||||
* 媒体数据流
|
||||
* @param mediaType
|
||||
* 媒体类型
|
||||
* @param fileName
|
||||
* 文件名
|
||||
* @return 上传到微信服务器返回的媒体标识
|
||||
* @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>
|
||||
@ -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>
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public String uploadMedia(int agentid, InputStream is, MediaType mediaType)
|
||||
public String uploadMedia(int agentid, InputStream is, String fileName)
|
||||
throws WeixinException {
|
||||
Token token = tokenHolder.getToken();
|
||||
if (mediaType == null) {
|
||||
mediaType = MediaType.file;
|
||||
} else if (mediaType == MediaType.thumb) {
|
||||
mediaType = MediaType.image;
|
||||
} else if (mediaType == MediaType.news) {
|
||||
throw new WeixinException(
|
||||
"please invoke uploadMaterialArticle method");
|
||||
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 {
|
||||
mediaType = MediaType.file;
|
||||
}
|
||||
Token token = tokenHolder.getToken();
|
||||
try {
|
||||
WeixinResponse response = null;
|
||||
if (agentid > 0) {
|
||||
@ -129,15 +114,17 @@ public class MediaApi extends QyApi {
|
||||
response = weixinClient.post(String.format(
|
||||
material_media_upload_uri, token.getAccessToken(),
|
||||
mediaType.name(), agentid), new FormBodyPart("media",
|
||||
new InputStreamBody(is, mediaType.getContentType()
|
||||
.getMimeType(), ObjectId.get().toHexString())));
|
||||
new InputStreamBody(new ByteArrayInputStream(content),
|
||||
mediaType.getContentType().getMimeType(),
|
||||
fileName)));
|
||||
} else {
|
||||
String file_upload_uri = getRequestUri("file_upload_uri");
|
||||
response = weixinClient.post(String.format(file_upload_uri,
|
||||
token.getAccessToken(), mediaType.name()),
|
||||
new FormBodyPart("media", new InputStreamBody(is,
|
||||
mediaType.getContentType().getMimeType(),
|
||||
ObjectId.get().toHexString())));
|
||||
new FormBodyPart("media", new InputStreamBody(
|
||||
new ByteArrayInputStream(content), mediaType
|
||||
.getContentType().getMimeType(),
|
||||
fileName)));
|
||||
}
|
||||
return response.getAsJson().getString("media_id");
|
||||
} finally {
|
||||
@ -468,7 +455,7 @@ public class MediaApi extends QyApi {
|
||||
writer.write("\r\n");
|
||||
}
|
||||
return uploadMedia(0, new ByteArrayInputStream(writer.getBuffer()
|
||||
.toString().getBytes(Consts.UTF_8)), MediaType.file);
|
||||
.toString().getBytes(Consts.UTF_8)), batchName);
|
||||
} finally {
|
||||
try {
|
||||
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.UserStatus;
|
||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||
import com.foxinmy.weixin4j.type.MediaType;
|
||||
|
||||
/**
|
||||
* 成员API
|
||||
@ -118,8 +117,7 @@ public class UserApi extends QyApi {
|
||||
obj.put("extattr", attrs);
|
||||
}
|
||||
if (avatar != null) {
|
||||
obj.put("avatar_mediaid",
|
||||
mediaApi.uploadMedia(0, avatar, MediaType.image));
|
||||
obj.put("avatar_mediaid", mediaApi.uploadMedia(0, avatar, null));
|
||||
}
|
||||
Token token = tokenHolder.getToken();
|
||||
WeixinResponse response = weixinClient.post(
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package com.foxinmy.weixin4j.qy.test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Assert;
|
||||
@ -31,8 +32,9 @@ public class MediaTest extends TokenTest {
|
||||
|
||||
@Test
|
||||
public void upload() throws IOException, WeixinException {
|
||||
File file = new File("//Users/jy/Downloads/import_file.csv");
|
||||
String mediaId = mediaApi.uploadMedia(3, file);
|
||||
File file = new File("/Users/jy/Downloads/uu-logo.png");
|
||||
String mediaId = mediaApi.uploadMedia(0, new FileInputStream(file),
|
||||
file.getName());
|
||||
// 1-1gpykXsR8bhNvO13-ZvskptCBxQF1UE535jFdCF63N2inGRAqEb-psF6eppjIIl
|
||||
// 1CF6sBgWWFGY9s4JCEet5ASszsTuyHpeN1f2LWXADveqBlKoxSgb3cO401NEM7dNY
|
||||
Assert.assertNotNull(mediaId);
|
||||
@ -42,7 +44,8 @@ public class MediaTest extends TokenTest {
|
||||
@Test
|
||||
public void download() throws WeixinException, IOException {
|
||||
File file = mediaApi
|
||||
.downloadMediaFile(3,
|
||||
.downloadMediaFile(
|
||||
3,
|
||||
"272LZlRmz1h7V2lcsvouCxwbJ_Dh-rgdDecX_26f_HDzJSZiSZjBeqeSYI1r9Ad9q66iWTGmRDUFgWOvz_fGVGi1BRZ4wjtkhPe2XcK-oomk");
|
||||
Assert.assertTrue(file.exists());
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user