媒体接口(MediaApi)中上传方法中的File类型调整为InputStream
This commit is contained in:
@@ -2,6 +2,7 @@ package com.foxinmy.weixin4j.mp;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@@ -132,7 +133,8 @@ public class WeixinProxy {
|
||||
* @param isMaterial
|
||||
* 是否永久上传
|
||||
* @return 上传到微信服务器返回的媒体标识
|
||||
* @see {@link com.foxinmy.weixin4j.mp.WeixinProxy#uploadMedia(File, MediaType)}
|
||||
* @see {@link com.foxinmy.weixin4j.mp.WeixinProxy#uploadMedia(InputStream, MediaType,boolean)}
|
||||
* @see com.foxinmy.weixin4j.mp.api.MediaApi
|
||||
* @throws WeixinException
|
||||
* @throws IOException
|
||||
*/
|
||||
@@ -142,49 +144,30 @@ public class WeixinProxy {
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传媒体文件
|
||||
*
|
||||
* @param file
|
||||
* 文件对象
|
||||
* @param mediaType
|
||||
* 媒体类型
|
||||
* @param isMaterial
|
||||
* 是否永久上传
|
||||
* @return 上传到微信服务器返回的媒体标识
|
||||
* @throws WeixinException
|
||||
* @throws IOException
|
||||
* @see com.foxinmy.weixin4j.type.MediaType
|
||||
* @see {@link com.foxinmy.weixin4j.mp.WeixinProxy#uploadMedia(String, byte[],String)}
|
||||
*/
|
||||
public String uploadMedia(File file, MediaType mediaType, boolean isMaterial)
|
||||
throws WeixinException, IOException {
|
||||
return mediaApi.uploadMedia(file, mediaType, isMaterial);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传媒体文件
|
||||
* 上传媒体文件 </br> <font color="red">此接口只包含图片、语音、缩略图、视频(临时)四种媒体类型的上传</font>
|
||||
* <p>
|
||||
* 正常情况下返回{"type":"TYPE","media_id":"MEDIA_ID","created_at":123456789},
|
||||
* 否则抛出异常.
|
||||
* </p>
|
||||
*
|
||||
* @param fileName
|
||||
* 文件名
|
||||
* @param data
|
||||
* 媒体数据包
|
||||
* @param is
|
||||
* 媒体数据流
|
||||
* @param mediaType
|
||||
* 媒体类型
|
||||
* 媒体文件类型:分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb)
|
||||
* @param isMaterial
|
||||
* 是否永久上传
|
||||
* @return 上传到微信服务器返回的媒体标识
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/10/78b15308b053286e2a66b33f0f0f5fb6.html">上传下载说明</a>
|
||||
* href="http://mp.weixin.qq.com/wiki/5/963fc70b80dc75483a271298a76a8d59.html">上传临时素材</a>
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/14/7e6c03263063f4813141c3e17dd4350a.html">上传永久素材</a>
|
||||
* @see com.foxinmy.weixin4j.type.MediaType
|
||||
* @see com.foxinmy.weixin4j.mp.api.MediaApi
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public String uploadMedia(String fileName, byte[] data, String mediaType,
|
||||
public String uploadMedia(InputStream is, MediaType mediaType,
|
||||
boolean isMaterial) throws WeixinException {
|
||||
return mediaApi.uploadMedia(fileName, data, mediaType, isMaterial);
|
||||
return mediaApi.uploadMedia(is, mediaType, isMaterial);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -203,13 +186,12 @@ public class WeixinProxy {
|
||||
* @throws WeixinException
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/10/78b15308b053286e2a66b33f0f0f5fb6.html">上传下载说明</a>
|
||||
* @see com.foxinmy.weixin4j.type.MediaType
|
||||
* @see com.foxinmy.weixin4j.mp.api.MediaApi
|
||||
* @see {@link com.foxinmy.weixin4j.mp.WeixinProxy#downloadMedia(String)}
|
||||
*/
|
||||
public File downloadMedia(String mediaId, MediaType mediaType,
|
||||
boolean isMaterial) throws WeixinException {
|
||||
return mediaApi.downloadMedia(mediaId, mediaType, isMaterial);
|
||||
public File downloadMediaFile(String mediaId, boolean isMaterial)
|
||||
throws WeixinException {
|
||||
return mediaApi.downloadMediaFile(mediaId, isMaterial);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,6 +4,7 @@ import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.ArrayList;
|
||||
@@ -16,6 +17,7 @@ import com.alibaba.fastjson.parser.deserializer.ExtraProcessor;
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.apache.ByteArrayBody;
|
||||
import com.foxinmy.weixin4j.http.apache.FormBodyPart;
|
||||
import com.foxinmy.weixin4j.http.apache.InputStreamBody;
|
||||
import com.foxinmy.weixin4j.http.apache.StringBody;
|
||||
import com.foxinmy.weixin4j.http.weixin.JsonResult;
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
||||
@@ -30,6 +32,7 @@ import com.foxinmy.weixin4j.type.MediaType;
|
||||
import com.foxinmy.weixin4j.util.ConfigUtil;
|
||||
import com.foxinmy.weixin4j.util.FileUtil;
|
||||
import com.foxinmy.weixin4j.util.IOUtil;
|
||||
import com.foxinmy.weixin4j.util.ObjectId;
|
||||
import com.foxinmy.weixin4j.util.StringUtil;
|
||||
|
||||
/**
|
||||
@@ -57,7 +60,7 @@ public class MediaApi extends MpApi {
|
||||
* @param isMaterial
|
||||
* 是否永久上传
|
||||
* @return 上传到微信服务器返回的媒体标识
|
||||
* @see {@link com.foxinmy.weixin4j.mp.api.MediaApi#uploadMedia(File, MediaType)}
|
||||
* @see {@link com.foxinmy.weixin4j.mp.api.MediaApi#uploadMedia(InputStream, MediaType,boolean)}
|
||||
* @throws WeixinException
|
||||
* @throws IOException
|
||||
*/
|
||||
@@ -67,28 +70,17 @@ public class MediaApi extends MpApi {
|
||||
if (StringUtil.isBlank(mediaTypeKey)) {
|
||||
mediaTypeKey = FileUtil.getFileType(file);
|
||||
}
|
||||
MediaType mediaType = MediaType.getMediaType(mediaTypeKey);
|
||||
return uploadMedia(file, mediaType, isMaterial);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传媒体文件</br> <font color="red">此接口只包含图片、语音、缩略图三种媒体类型的上传</font>
|
||||
*
|
||||
* @param file
|
||||
* 文件对象
|
||||
* @param mediaType
|
||||
* 媒体类型 (image)、语音(voice)和缩略图(thumb)
|
||||
* @param isMaterial
|
||||
* 是否永久上传
|
||||
* @return 上传到微信服务器返回的媒体标识
|
||||
* @throws WeixinException
|
||||
* @see com.foxinmy.weixin4j.type.MediaType
|
||||
* @see {@link com.foxinmy.weixin4j.mp.api.MediaApi#uploadMedia(String, byte[],String,boolean)}
|
||||
*/
|
||||
public String uploadMedia(File file, MediaType mediaType, boolean isMaterial)
|
||||
throws WeixinException, IOException {
|
||||
byte[] datas = IOUtil.toByteArray(new FileInputStream(file));
|
||||
return uploadMedia(file.getName(), datas, mediaType.name(), isMaterial);
|
||||
MediaType mediaType = null;
|
||||
if ("bmp/png/jpeg/jpg/gif".contains(mediaTypeKey)) {
|
||||
mediaType = MediaType.image;
|
||||
} else if ("amr/mp3".contains(mediaTypeKey)) {
|
||||
mediaType = MediaType.voice;
|
||||
} else if ("mp3/wma/wav/amr".equals(mediaTypeKey)) {
|
||||
mediaType = MediaType.video;
|
||||
} else {
|
||||
throw new WeixinException("unknown mediaType:" + mediaTypeKey);
|
||||
}
|
||||
return uploadMedia(new FileInputStream(file), mediaType, isMaterial);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -98,10 +90,8 @@ public class MediaApi extends MpApi {
|
||||
* 否则抛出异常.
|
||||
* </p>
|
||||
*
|
||||
* @param fileName
|
||||
* 文件名
|
||||
* @param bytes
|
||||
* 媒体数据包
|
||||
* @param is
|
||||
* 媒体数据流
|
||||
* @param mediaType
|
||||
* 媒体文件类型:分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb)
|
||||
* @param isMaterial
|
||||
@@ -111,37 +101,45 @@ public class MediaApi extends MpApi {
|
||||
* href="http://mp.weixin.qq.com/wiki/5/963fc70b80dc75483a271298a76a8d59.html">上传临时素材</a>
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/14/7e6c03263063f4813141c3e17dd4350a.html">上传永久素材</a>
|
||||
* @see com.foxinmy.weixin4j.type.MediaType
|
||||
* @see com.foxinmy.weixin4j.mp.api.MediaApi
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public String uploadMedia(String fileName, byte[] bytes, String mediaType,
|
||||
public String uploadMedia(InputStream is, MediaType mediaType,
|
||||
boolean isMaterial) throws WeixinException {
|
||||
if (",image,voice,video,thumb,".indexOf(String
|
||||
.format(",%s,", mediaType)) < 0) {
|
||||
throw new WeixinException(String.format(
|
||||
"unsupported media type:%s", mediaType));
|
||||
}
|
||||
if (mediaType.equals(MediaType.video.name()) && isMaterial) {
|
||||
throw new WeixinException(
|
||||
"please invoke uploadMaterialVideo method");
|
||||
}
|
||||
Token token = tokenHolder.getToken();
|
||||
WeixinResponse response = null;
|
||||
if (isMaterial) {
|
||||
String material_media_upload_uri = getRequestUri("material_media_upload_uri");
|
||||
try {
|
||||
response = weixinClient.post(String.format(
|
||||
material_media_upload_uri, token.getAccessToken()),
|
||||
new FormBodyPart("media", new ByteArrayBody(bytes,
|
||||
fileName)), new FormBodyPart("type",
|
||||
new StringBody(mediaType, Consts.UTF_8)));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new WeixinException(e); // ignore
|
||||
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)));
|
||||
} else {
|
||||
String file_upload_uri = getRequestUri("file_upload_uri");
|
||||
response = weixinClient.post(String.format(file_upload_uri,
|
||||
token.getAccessToken(), mediaType), new FormBodyPart(
|
||||
"media", new InputStreamBody(is, mediaType
|
||||
.getContentType().getMimeType(), ObjectId.get()
|
||||
.toHexString())));
|
||||
}
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
; // ignore
|
||||
} finally {
|
||||
try {
|
||||
is.close();
|
||||
} catch (IOException e) {
|
||||
;
|
||||
}
|
||||
} else {
|
||||
String file_upload_uri = getRequestUri("file_upload_uri");
|
||||
response = weixinClient.post(String.format(file_upload_uri,
|
||||
token.getAccessToken(), mediaType), new FormBodyPart(
|
||||
"media", new ByteArrayBody(bytes, fileName)));
|
||||
}
|
||||
return response.getAsJson().getString("media_id");
|
||||
}
|
||||
@@ -154,27 +152,18 @@ public class MediaApi extends MpApi {
|
||||
*
|
||||
* @param mediaId
|
||||
* 存储在微信服务器上的媒体标识
|
||||
* @param mediaType
|
||||
* 媒体文件类型:分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb)
|
||||
* @return 写入硬盘后的文件对象
|
||||
* @return 写入硬盘后的文件对象,存储路径见weixin4j.properties配置
|
||||
* @throws WeixinException
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/11/07b6b76a6b6e8848e855a435d5e34a5f.html">下载临时媒体文件</a>
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/4/b3546879f07623cb30df9ca0e420a5d0.html">下载永久媒体素材</a>
|
||||
* @see com.foxinmy.weixin4j.type.MediaType
|
||||
* @see {@link com.foxinmy.weixin4j.mp.api.MediaApi#downloadMedia(String,boolean)}
|
||||
*/
|
||||
public File downloadMedia(String mediaId, MediaType mediaType,
|
||||
boolean isMaterial) throws WeixinException {
|
||||
if (",image,voice,video,thumb,".indexOf(String.format(",%s,",
|
||||
mediaType.name())) < 0) {
|
||||
throw new WeixinException(String.format(
|
||||
"unsupported media type:%s", mediaType.name()));
|
||||
}
|
||||
public File downloadMediaFile(String mediaId, boolean isMaterial)
|
||||
throws WeixinException {
|
||||
String media_path = ConfigUtil.getValue("media_path");
|
||||
File file = new File(media_path + File.separator + mediaId + "."
|
||||
+ mediaType.getFormatName());
|
||||
File file = new File(media_path + File.separator + mediaId);
|
||||
if (file.exists()) {
|
||||
return file;
|
||||
}
|
||||
@@ -195,7 +184,7 @@ public class MediaApi extends MpApi {
|
||||
if (os != null) {
|
||||
os.close();
|
||||
}
|
||||
} catch (IOException ignore) {
|
||||
} catch (IOException e) {
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@ import com.foxinmy.weixin4j.tuple.Image;
|
||||
import com.foxinmy.weixin4j.tuple.MpArticle;
|
||||
import com.foxinmy.weixin4j.tuple.Text;
|
||||
import com.foxinmy.weixin4j.tuple.Video;
|
||||
import com.foxinmy.weixin4j.type.MediaType;
|
||||
|
||||
/**
|
||||
* 群发消息
|
||||
@@ -42,7 +41,7 @@ public class MassTest extends TokenTest {
|
||||
public void uploadArticle() throws IOException, WeixinException {
|
||||
List<MpArticle> articles = new ArrayList<MpArticle>();
|
||||
String thumbMediaId = mediaApi.uploadMedia(new File("/tmp/test.jpg"),
|
||||
MediaType.image, false);
|
||||
false);
|
||||
articles.add(new MpArticle(thumbMediaId, "title", "content"));
|
||||
massApi.uploadArticle(articles);
|
||||
}
|
||||
@@ -71,7 +70,7 @@ public class MassTest extends TokenTest {
|
||||
public void massArticleByGroup() throws IOException, WeixinException {
|
||||
List<MpArticle> articles = new ArrayList<MpArticle>();
|
||||
String thumbMediaId = mediaApi.uploadMedia(new File("/tmp/test.jpg"),
|
||||
MediaType.image, false);
|
||||
false);
|
||||
articles.add(new MpArticle(thumbMediaId, "title", "content"));
|
||||
String massId = massApi.massArticleByGroupId(articles, 0);
|
||||
Assert.assertTrue(massId != null);
|
||||
@@ -81,7 +80,7 @@ public class MassTest extends TokenTest {
|
||||
public void massArticleByOpenIds() throws IOException, WeixinException {
|
||||
List<MpArticle> articles = new ArrayList<MpArticle>();
|
||||
String thumbMediaId = mediaApi.uploadMedia(new File("/tmp/test.jpg"),
|
||||
MediaType.image, false);
|
||||
false);
|
||||
articles.add(new MpArticle(thumbMediaId, "title", "content"));
|
||||
String massId = massApi.massArticleByOpenIds(articles,
|
||||
"owGBft_vbBbOaQOmpEUE4xDLeRSU");
|
||||
|
||||
@@ -39,7 +39,7 @@ 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, MediaType.image, false);
|
||||
String mediaId = mediaApi.uploadMedia(file, false);
|
||||
// 1Vgd1R5DdznSc3rPxd-sNZ3pLt54cejhJ5ItuNcCgrqoQArNANWy5oxso_r9KNlE
|
||||
Assert.assertNotNull(mediaId);
|
||||
System.err.println(mediaId);
|
||||
@@ -48,16 +48,16 @@ public class MediaTest extends TokenTest {
|
||||
@Test
|
||||
public void download1() throws WeixinException, IOException {
|
||||
File file = mediaApi
|
||||
.downloadMedia(
|
||||
.downloadMediaFile(
|
||||
"1Vgd1R5DdznSc3rPxd-sNZ3pLt54cejhJ5ItuNcCgrqoQArNANWy5oxso_r9KNlE",
|
||||
MediaType.image, false);
|
||||
false);
|
||||
Assert.assertTrue(file.exists());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void upload2() throws IOException, WeixinException {
|
||||
File file = new File("/Users/jy/Downloads/test.jpg");
|
||||
String mediaId = mediaApi.uploadMedia(file, MediaType.image, true);
|
||||
String mediaId = mediaApi.uploadMedia(file, true);
|
||||
// 8790403529
|
||||
Assert.assertNotNull(mediaId);
|
||||
System.err.println(mediaId);
|
||||
@@ -85,7 +85,7 @@ public class MediaTest extends TokenTest {
|
||||
|
||||
@Test
|
||||
public void download2() throws WeixinException, IOException {
|
||||
File file = mediaApi.downloadMedia("8790403529", MediaType.image, true);
|
||||
File file = mediaApi.downloadMediaFile("8790403529", true);
|
||||
Assert.assertTrue(file.exists());
|
||||
}
|
||||
|
||||
@@ -129,10 +129,11 @@ public class MediaTest extends TokenTest {
|
||||
20);
|
||||
System.err.println(mediaRecord);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void listAllMaterialMedia() throws WeixinException {
|
||||
List<MediaItem> mediaList = mediaApi.listAllMaterialMedia(MediaType.image);
|
||||
List<MediaItem> mediaList = mediaApi
|
||||
.listAllMaterialMedia(MediaType.image);
|
||||
System.err.println(mediaList);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@ import com.foxinmy.weixin4j.tuple.News;
|
||||
import com.foxinmy.weixin4j.tuple.Text;
|
||||
import com.foxinmy.weixin4j.tuple.Video;
|
||||
import com.foxinmy.weixin4j.tuple.Voice;
|
||||
import com.foxinmy.weixin4j.type.MediaType;
|
||||
|
||||
/**
|
||||
* 客服消息测试
|
||||
@@ -90,8 +89,7 @@ public class NotifyTest extends TokenTest {
|
||||
|
||||
@Test
|
||||
public void send2() throws WeixinException, IOException {
|
||||
String mediaId = mediaApi.uploadMedia(new File("/tmp/test.jpg"),
|
||||
MediaType.image, false);
|
||||
String mediaId = mediaApi.uploadMedia(new File("/tmp/test.jpg"), false);
|
||||
NotifyMessage imageNotify = new NotifyMessage(
|
||||
"owGBft_vbBbOaQOmpEUE4xDLeRSU", new Image(mediaId));
|
||||
JsonResult result = notifyApi.sendNotify(imageNotify);
|
||||
|
||||
Reference in New Issue
Block a user