public String convertFileToBase64(String imgPath) {
byte[] data = null;
// 读取图片字节数组
ByteArrayOutputStream out = new ByteArrayOutputStream();
//这里如果用stringbuffer 会有问题
try {
URL url = new URL(domain+imgPath);
URLConnection con = url.openConnection();
con.setReadTimeout(60000);
con.setConnectTimeout(60000);
InputStream in = con.getInputStream();
final int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int rsz = -1;
while ((rsz = in.read(buffer))>-1) {
out.write(buffer,0,rsz);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
data = out.toByteArray();
// 对字节数组进行Base64编码,得到Base64编码的字符串
BASE64Encoder encoder = new BASE64Encoder();
String base64
文件流读取和写入,base64转换
最新推荐文章于 2024-06-27 19:57:47 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)