媒体接口(MediaApi)中上传方法中的File类型调整为InputStream
This commit is contained in:
@@ -2,6 +2,7 @@ package com.foxinmy.weixin4j.qy;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
@@ -207,7 +208,7 @@ public class WeixinProxy {
|
||||
* 媒体对象
|
||||
* @return 上传到微信服务器返回的媒体标识
|
||||
* @see com.foxinmy.weixin4j.qy.api.MediaApi
|
||||
* @see {@link com.foxinmy.weixin4j.qy.WeixinProxy.MediaApi#uploadMedia(File, MediaType)}
|
||||
* @see {@link com.foxinmy.weixin4j.qy.WeixinProxy.MediaApi#uploadMedia(InputStream, MediaType)}
|
||||
* @throws WeixinException
|
||||
* @throws IOException
|
||||
*/
|
||||
@@ -215,25 +216,6 @@ public class WeixinProxy {
|
||||
return mediaApi.uploadMedia(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传媒体文件
|
||||
*
|
||||
* @param file
|
||||
* 文件对象
|
||||
* @param mediaType
|
||||
* 媒体类型
|
||||
* @return 上传到微信服务器返回的媒体标识
|
||||
* @throws WeixinException
|
||||
* @throws IOException
|
||||
* @see com.foxinmy.weixin4j.qy.api.MediaApi
|
||||
* @see com.foxinmy.weixin4j.type.MediaType
|
||||
* @see {@link com.foxinmy.weixin4j.qy.WeixinProxy#uploadMedia(String, byte[],String)}
|
||||
*/
|
||||
public String uploadMedia(File file, MediaType mediaType)
|
||||
throws WeixinException, IOException {
|
||||
return mediaApi.uploadMedia(file, mediaType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传媒体文件(完全公开。所有管理员均可调用,media_id可以共享)
|
||||
* <p>
|
||||
@@ -241,19 +223,20 @@ public class WeixinProxy {
|
||||
* 否则抛出异常.
|
||||
* </p>
|
||||
*
|
||||
* @param bytes
|
||||
* 媒体数据包
|
||||
* @param is
|
||||
* 媒体数据流
|
||||
* @param mediaType
|
||||
* 媒体类型
|
||||
* @return 上传到微信服务器返回的媒体标识
|
||||
* @see com.foxinmy.weixin4j.qy.api.MediaApi
|
||||
* @see com.foxinmy.weixin4j.type.MediaType
|
||||
* @return 上传到微信服务器返回的媒体标识
|
||||
* @see <a
|
||||
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E4%B8%8A%E4%BC%A0%E5%AA%92%E4%BD%93%E6%96%87%E4%BB%B6">上传媒体文件说明</a>
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public String uploadMedia(String fileName, byte[] bytes, String mediaType)
|
||||
public String uploadMedia(InputStream is, MediaType mediaType)
|
||||
throws WeixinException {
|
||||
return mediaApi.uploadMedia(fileName, bytes, mediaType);
|
||||
return mediaApi.uploadMedia(is, mediaType);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -279,9 +262,7 @@ public class WeixinProxy {
|
||||
*
|
||||
* @param mediaId
|
||||
* 存储在微信服务器上的媒体标识
|
||||
* @param extension
|
||||
* 媒体后缀名
|
||||
* @return 写入硬盘后的文件对象
|
||||
* @return 写入硬盘后的文件对象,存储路径见weixin4j.properties配置
|
||||
* @throws WeixinException
|
||||
* @throws IOException
|
||||
* @see com.foxinmy.weixin4j.qy.api.MediaApi
|
||||
@@ -290,9 +271,8 @@ public class WeixinProxy {
|
||||
* @see com.foxinmy.weixin4j.type.MediaType
|
||||
* @see {@link com.foxinmy.weixin4j.WeixinProxy.MediaApi#downloadMedia(String)}
|
||||
*/
|
||||
public File downloadMedia(String mediaId, String extension)
|
||||
throws WeixinException {
|
||||
return mediaApi.downloadMedia(mediaId, extension);
|
||||
public File downloadMediaFile(String mediaId) throws WeixinException {
|
||||
return mediaApi.downloadMediaFile(mediaId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -406,7 +386,8 @@ public class WeixinProxy {
|
||||
* @return 处理结果
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public JsonResult createUser(User user, File avatar) throws WeixinException {
|
||||
public JsonResult createUser(User user, InputStream avatar)
|
||||
throws WeixinException {
|
||||
return userApi.createUser(user, avatar);
|
||||
}
|
||||
|
||||
@@ -440,7 +421,8 @@ public class WeixinProxy {
|
||||
* @return 处理结果
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public JsonResult updateUser(User user, File avatar) throws WeixinException {
|
||||
public JsonResult updateUser(User user, InputStream avatar)
|
||||
throws WeixinException {
|
||||
return userApi.updateUser(user, avatar);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
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;
|
||||
import java.io.OutputStream;
|
||||
import java.io.StringWriter;
|
||||
import java.util.LinkedHashMap;
|
||||
@@ -15,8 +17,8 @@ import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson.serializer.PropertyFilter;
|
||||
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.weixin.WeixinResponse;
|
||||
import com.foxinmy.weixin4j.model.Consts;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
@@ -28,6 +30,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;
|
||||
|
||||
/**
|
||||
@@ -55,7 +58,7 @@ public class MediaApi extends QyApi {
|
||||
* @param file
|
||||
* 媒体对象
|
||||
* @return 上传到微信服务器返回的媒体标识
|
||||
* @see {@link com.foxinmy.weixin4j.qy.api.MediaApi#uploadMedia(File, MediaType)}
|
||||
* @see {@link com.foxinmy.weixin4j.qy.api.MediaApi#uploadMedia(InputStream, MediaType)}
|
||||
* @throws WeixinException
|
||||
* @throws IOException
|
||||
*/
|
||||
@@ -64,27 +67,17 @@ public class MediaApi extends QyApi {
|
||||
if (StringUtil.isBlank(mediaTypeKey)) {
|
||||
mediaTypeKey = FileUtil.getFileType(file);
|
||||
}
|
||||
MediaType mediaType = MediaType.getMediaType(mediaTypeKey);
|
||||
return uploadMedia(file, mediaType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传媒体文件
|
||||
*
|
||||
* @param file
|
||||
* 文件对象
|
||||
* @param mediaType
|
||||
* 媒体类型
|
||||
* @return 上传到微信服务器返回的媒体标识
|
||||
* @throws WeixinException
|
||||
* @throws IOException
|
||||
* @see com.foxinmy.weixin4j.type.MediaType
|
||||
* @see {@link com.foxinmy.weixin4j.qy.api.MediaApi#uploadMedia(String, byte[],String)}
|
||||
*/
|
||||
public String uploadMedia(File file, MediaType mediaType)
|
||||
throws WeixinException, IOException {
|
||||
byte[] datas = IOUtil.toByteArray(new FileInputStream(file));
|
||||
return uploadMedia(file.getName(), datas, mediaType.name());
|
||||
MediaType mediaType = null;
|
||||
if (mediaTypeKey.equals("jpg")) {
|
||||
mediaType = MediaType.image;
|
||||
} else if ("amr/mp3".contains(mediaTypeKey)) {
|
||||
mediaType = MediaType.voice;
|
||||
} else if (mediaTypeKey.equals("mp4")) {
|
||||
mediaType = MediaType.video;
|
||||
} else {
|
||||
mediaType = MediaType.file;
|
||||
}
|
||||
return uploadMedia(new FileInputStream(file), mediaType);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -94,8 +87,8 @@ public class MediaApi extends QyApi {
|
||||
* 否则抛出异常.
|
||||
* </p>
|
||||
*
|
||||
* @param bytes
|
||||
* 媒体数据包
|
||||
* @param is
|
||||
* 媒体数据流
|
||||
* @param mediaType
|
||||
* 媒体类型
|
||||
* @return 上传到微信服务器返回的媒体标识
|
||||
@@ -103,14 +96,25 @@ public class MediaApi extends QyApi {
|
||||
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E4%B8%8A%E4%BC%A0%E5%AA%92%E4%BD%93%E6%96%87%E4%BB%B6">上传媒体文件说明</a>
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public String uploadMedia(String fileName, byte[] bytes, String mediaType)
|
||||
public String uploadMedia(InputStream is, MediaType mediaType)
|
||||
throws WeixinException {
|
||||
Token token = tokenHolder.getToken();
|
||||
String file_upload_uri = getRequestUri("file_upload_uri");
|
||||
WeixinResponse response = weixinClient.post(String.format(file_upload_uri,
|
||||
token.getAccessToken(), mediaType), new FormBodyPart("media",
|
||||
new ByteArrayBody(bytes, fileName)));
|
||||
|
||||
Token token = tokenHolder.getToken();
|
||||
if (mediaType == null || mediaType == MediaType.news) {
|
||||
mediaType = MediaType.file;
|
||||
} else if (mediaType == MediaType.thumb) {
|
||||
mediaType = MediaType.image;
|
||||
}
|
||||
WeixinResponse response = weixinClient.post(String.format(
|
||||
file_upload_uri, token.getAccessToken(), mediaType),
|
||||
new FormBodyPart("media", new InputStreamBody(is, mediaType
|
||||
.getContentType().getMimeType(), ObjectId.get()
|
||||
.toHexString())));
|
||||
try {
|
||||
is.close();
|
||||
} catch (IOException e) {
|
||||
;
|
||||
}
|
||||
return response.getAsJson().getString("media_id");
|
||||
}
|
||||
|
||||
@@ -122,9 +126,7 @@ public class MediaApi extends QyApi {
|
||||
*
|
||||
* @param mediaId
|
||||
* 存储在微信服务器上的媒体标识
|
||||
* @param extension
|
||||
* 媒体后缀名
|
||||
* @return 写入硬盘后的文件对象
|
||||
* @return 写入硬盘后的文件对象,存储路径见weixin4j.properties配置
|
||||
* @throws WeixinException
|
||||
* @throws IOException
|
||||
* @see <a
|
||||
@@ -132,11 +134,9 @@ public class MediaApi extends QyApi {
|
||||
* @see com.foxinmy.weixin4j.type.MediaType
|
||||
* @see {@link com.foxinmy.weixin4j.qy.api.MediaApi#downloadMedia(String)}
|
||||
*/
|
||||
public File downloadMedia(String mediaId, String extension)
|
||||
throws WeixinException {
|
||||
public File downloadMediaFile(String mediaId) throws WeixinException {
|
||||
String media_path = ConfigUtil.getValue("media_path");
|
||||
File file = new File(media_path + File.separator + mediaId + "."
|
||||
+ extension);
|
||||
File file = new File(media_path + File.separator + mediaId);
|
||||
if (file.exists()) {
|
||||
return file;
|
||||
}
|
||||
@@ -158,7 +158,7 @@ public class MediaApi extends QyApi {
|
||||
if (os != null) {
|
||||
os.close();
|
||||
}
|
||||
} catch (IOException ignore) {
|
||||
} catch (IOException e) {
|
||||
;
|
||||
}
|
||||
}
|
||||
@@ -178,8 +178,8 @@ public class MediaApi extends QyApi {
|
||||
public byte[] downloadMedia(String mediaId) throws WeixinException {
|
||||
Token token = tokenHolder.getToken();
|
||||
String file_download_uri = getRequestUri("file_download_uri");
|
||||
WeixinResponse response = weixinClient.get(String.format(file_download_uri,
|
||||
token.getAccessToken(), mediaId));
|
||||
WeixinResponse response = weixinClient.get(String.format(
|
||||
file_download_uri, token.getAccessToken(), mediaId));
|
||||
return response.getContent();
|
||||
}
|
||||
|
||||
@@ -239,8 +239,8 @@ public class MediaApi extends QyApi {
|
||||
writer.write(StringUtil.join(column.values(), ','));
|
||||
writer.write("\r\n");
|
||||
}
|
||||
String mediaId = uploadMedia(batchName,
|
||||
writer.toString().getBytes(Consts.UTF_8), MediaType.file.name());
|
||||
String mediaId = uploadMedia(new ByteArrayInputStream(writer
|
||||
.getBuffer().toString().getBytes(Consts.UTF_8)), MediaType.file);
|
||||
try {
|
||||
writer.close();
|
||||
} catch (IOException e) {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.foxinmy.weixin4j.qy.api;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -17,6 +16,7 @@ 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
|
||||
@@ -67,7 +67,8 @@ public class UserApi extends QyApi {
|
||||
* @return 处理结果
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public JsonResult createUser(User user, File avatar) throws WeixinException {
|
||||
public JsonResult createUser(User user, InputStream avatar)
|
||||
throws WeixinException {
|
||||
String user_create_uri = getRequestUri("user_create_uri");
|
||||
return excute(user_create_uri, user, avatar);
|
||||
}
|
||||
@@ -101,12 +102,13 @@ public class UserApi extends QyApi {
|
||||
* @return 处理结果
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public JsonResult updateUser(User user, File avatar) throws WeixinException {
|
||||
public JsonResult updateUser(User user, InputStream avatar)
|
||||
throws WeixinException {
|
||||
String user_update_uri = getRequestUri("user_update_uri");
|
||||
return excute(user_update_uri, user, avatar);
|
||||
}
|
||||
|
||||
private JsonResult excute(String uri, User user, File avatar)
|
||||
private JsonResult excute(String uri, User user, InputStream avatar)
|
||||
throws WeixinException {
|
||||
JSONObject obj = (JSONObject) JSON.toJSON(user);
|
||||
Object extattr = obj.remove("extattr");
|
||||
@@ -116,11 +118,8 @@ public class UserApi extends QyApi {
|
||||
obj.put("extattr", attrs);
|
||||
}
|
||||
if (avatar != null) {
|
||||
try {
|
||||
obj.put("avatar_mediaid", mediaApi.uploadMedia(avatar));
|
||||
} catch (IOException e) {
|
||||
throw new WeixinException(e);
|
||||
}
|
||||
obj.put("avatar_mediaid",
|
||||
mediaApi.uploadMedia(avatar, MediaType.image));
|
||||
}
|
||||
Token token = tokenHolder.getToken();
|
||||
WeixinResponse response = weixinClient.post(
|
||||
|
||||
Reference in New Issue
Block a user