diff --git a/src/main/java/com/hotlcc/wechat4j/Wechat.java b/src/main/java/com/hotlcc/wechat4j/Wechat.java index a047e72..0c3c863 100644 --- a/src/main/java/com/hotlcc/wechat4j/Wechat.java +++ b/src/main/java/com/hotlcc/wechat4j/Wechat.java @@ -1,5 +1,6 @@ package com.hotlcc.wechat4j; +import cn.hutool.core.io.FileUtil; import cn.hutool.core.thread.ThreadUtil; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSONArray; @@ -17,7 +18,6 @@ import com.hotlcc.wechat4j.model.MediaMessage; import com.hotlcc.wechat4j.model.ReceivedMsg; import com.hotlcc.wechat4j.model.UserInfo; import com.hotlcc.wechat4j.model.WxMessage; -import com.hotlcc.wechat4j.util.FileUtil; import com.hotlcc.wechat4j.util.PropertiesUtil; import com.hotlcc.wechat4j.util.QRCodeUtil; import com.hotlcc.wechat4j.util.WebWeixinApiUtil; @@ -1350,8 +1350,8 @@ public class Wechat { * @return 返回数据 */ public JSONObject sendImageToUserName(String userName, File image) { - ContentType contentType = FileUtil.getContentType(image); - byte[] mediaData = FileUtil.getBytes(image); + ContentType contentType = WechatUtil.getContentType(image); + byte[] mediaData = FileUtil.readBytes(image); return sendImageToUserName(userName, mediaData, image.getName(), contentType); } @@ -1390,8 +1390,8 @@ public class Wechat { * @return 返回数据 */ public JSONObject sendImageToNickName(String nickName, File image) { - ContentType contentType = FileUtil.getContentType(image); - byte[] mediaData = FileUtil.getBytes(image); + ContentType contentType = WechatUtil.getContentType(image); + byte[] mediaData = FileUtil.readBytes(image); return sendImageToNickName(nickName, mediaData, image.getName(), contentType); } @@ -1430,8 +1430,8 @@ public class Wechat { * @return 返回数据 */ public JSONObject sendImageToRemarkName(String remarkName, File image) { - ContentType contentType = FileUtil.getContentType(image); - byte[] mediaData = FileUtil.getBytes(image); + ContentType contentType = WechatUtil.getContentType(image); + byte[] mediaData = FileUtil.readBytes(image); return sendImageToRemarkName(remarkName, mediaData, image.getName(), contentType); } @@ -1480,8 +1480,8 @@ public class Wechat { * @return 返回数据 */ public JSONObject sendImage(String userName, String nickName, String remarkName, File image) { - ContentType contentType = FileUtil.getContentType(image); - byte[] mediaData = FileUtil.getBytes(image); + ContentType contentType = WechatUtil.getContentType(image); + byte[] mediaData = FileUtil.readBytes(image); return sendImage(userName, nickName, remarkName, mediaData, image.getName(), contentType); } @@ -1542,8 +1542,8 @@ public class Wechat { * @return 返回数据 */ public JSONObject sendVideoToUserName(String userName, File video) { - ContentType contentType = FileUtil.getContentType(video); - byte[] mediaData = FileUtil.getBytes(video); + ContentType contentType = WechatUtil.getContentType(video); + byte[] mediaData = FileUtil.readBytes(video); return sendVideoToUserName(userName, mediaData, video.getName(), contentType); } @@ -1582,8 +1582,8 @@ public class Wechat { * @return 返回数据 */ public JSONObject sendVideoToNickName(String nickName, File video) { - ContentType contentType = FileUtil.getContentType(video); - byte[] mediaData = FileUtil.getBytes(video); + ContentType contentType = WechatUtil.getContentType(video); + byte[] mediaData = FileUtil.readBytes(video); return sendVideoToNickName(nickName, mediaData, video.getName(), contentType); } @@ -1622,8 +1622,8 @@ public class Wechat { * @return 返回数据 */ public JSONObject sendVideoToRemarkName(String remarkName, File video) { - ContentType contentType = FileUtil.getContentType(video); - byte[] mediaData = FileUtil.getBytes(video); + ContentType contentType = WechatUtil.getContentType(video); + byte[] mediaData = FileUtil.readBytes(video); return sendVideoToRemarkName(remarkName, mediaData, video.getName(), contentType); } @@ -1672,8 +1672,8 @@ public class Wechat { * @return 返回数据 */ public JSONObject sendVideo(String userName, String nickName, String remarkName, File video) { - ContentType contentType = FileUtil.getContentType(video); - byte[] mediaData = FileUtil.getBytes(video); + ContentType contentType = WechatUtil.getContentType(video); + byte[] mediaData = FileUtil.readBytes(video); return sendVideo(userName, nickName, remarkName, mediaData, video.getName(), contentType); } } diff --git a/src/main/java/com/hotlcc/wechat4j/util/FileUtil.java b/src/main/java/com/hotlcc/wechat4j/util/FileUtil.java deleted file mode 100644 index aad54aa..0000000 --- a/src/main/java/com/hotlcc/wechat4j/util/FileUtil.java +++ /dev/null @@ -1,82 +0,0 @@ -package com.hotlcc.wechat4j.util; - -import lombok.extern.slf4j.Slf4j; -import org.apache.http.entity.ContentType; - -import javax.activation.MimetypesFileTypeMap; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; - -/** - * 文件工具类 - * - * @author Allen - */ -@Slf4j -public final class FileUtil { - private FileUtil() { - } - - /** - * 从文件中获取二进制数据 - * - * @param file 文件 - * @return 二进制数据 - */ - public static byte[] getBytes(File file) { - if (file == null) { - throw new IllegalArgumentException("参数file不能为null"); - } - - FileInputStream fis = null; - ByteArrayOutputStream baos = null; - try { - fis = new FileInputStream(file); - baos = new ByteArrayOutputStream(); - - byte[] buffer = new byte[1024]; - int len; - - while ((len = fis.read(buffer)) != -1) { - baos.write(buffer, 0, len); - } - - baos.flush(); - baos.close(); - fis.close(); - - return baos.toByteArray(); - } catch (IOException e) { - throw new RuntimeException(e); - } finally { - if (fis != null) { - try { - fis.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - if (baos != null) { - try { - baos.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - } - - /** - * 获取文件的ContentType - * - * @param file 文件 - * @return ContentType - */ - public static ContentType getContentType(File file) { - String mimeType = new MimetypesFileTypeMap().getContentType(file); - ContentType contentType = ContentType.parse(mimeType); - return contentType; - } -} diff --git a/src/main/java/com/hotlcc/wechat4j/util/WechatUtil.java b/src/main/java/com/hotlcc/wechat4j/util/WechatUtil.java index 1d809a1..2ec16df 100644 --- a/src/main/java/com/hotlcc/wechat4j/util/WechatUtil.java +++ b/src/main/java/com/hotlcc/wechat4j/util/WechatUtil.java @@ -3,6 +3,10 @@ package com.hotlcc.wechat4j.util; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import org.apache.commons.lang3.RandomStringUtils; +import org.apache.http.entity.ContentType; + +import javax.activation.MimetypesFileTypeMap; +import java.io.File; /** * 微信工具类 @@ -56,4 +60,16 @@ public final class WechatUtil { } return synckey.toString(); } + + /** + * 获取文件的ContentType + * + * @param file 文件 + * @return ContentType + */ + public static ContentType getContentType(File file) { + String mimeType = new MimetypesFileTypeMap().getContentType(file); + ContentType contentType = ContentType.parse(mimeType); + return contentType; + } }