Skip to content

Commit

Permalink
binarywang#1720 增加文件流生成base64方法,用于图片转base64,群机器人图片消息发送测试
Browse files Browse the repository at this point in the history
  • Loading branch information
yang ran committed Aug 21, 2020
1 parent 20f7363 commit 1a5d7ee
Showing 1 changed file with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.Base64;

public class FileUtils {

Expand Down Expand Up @@ -34,4 +35,32 @@ public static File createTmpFile(InputStream inputStream, String name, String ex
return createTmpFile(inputStream, name, ext, Files.createTempDirectory("weixin-java-tools-temp").toFile());
}

/**
* 文件流生成base64
*
* @param in 文件流
* @return base64编码
*/
public static String imageToBase64ByStream(InputStream in) {
byte[] data = null;
// 读取图片字节数组
try {
data = new byte[in.available()];
in.read(data);
// 返回Base64编码过的字节数组字符串
return Base64.getEncoder().encodeToString(data);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}

}

0 comments on commit 1a5d7ee

Please sign in to comment.