java 读取文件的字节数组
2024-10-16 11:30:52
/*文件64位编码*/
public static void main(String[] args) {
byte[] fileByte = toByteArray(newFile);
String imgStr = new BASE64Encoder().encode(fileByte);
}
/*读取文件的字节数组*/
public static byte[] toByteArray(File file) throws IOException {
File f = file;
if (!f.exists()) {
throw new FileNotFoundException("file not exists");
}
ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length());
BufferedInputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(f));
int buf_size = 1024;
byte[] buffer = new byte[buf_size];
int len = 0;
while (-1 != (len = in.read(buffer, 0, buf_size))) {
bos.write(buffer, 0, len);
}
return bos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
throw e;
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
bos.close();
}
}
最新文章
- 一步一步开发Game服务器(五)地图寻路
- SQL Server占用内存的认识
- xml、文件操作功能类
- JS时间戳格式化日期时间 由于mysql数据库里面存储时间存的是时间戳,取出来之后,JS要格式化一下显示。
- HBase HMaster Architecture - HBase Master架构
- 多个分布式系统如何共享使用一个固定公网IP
- OpenReports中文支持方案
- ANCS协议翻译
- 【转】20个令人敬畏的jQuery插件
- 201521123020 《Java程序设计》第9周学习总结
- C#中的协变(Covariance)和逆变(Contravariance)
- 转:IT巨头纷纷“卡位” 智能语音成人机交互入口必争之地
- SSIS - 2.使用脚本任务弹出对话框
- Ultimate Facebook Messenger for Business Guide (Feb 2019)
- java内存分配与垃圾回收
- 3D Slicer Reconstruct CT/MRI
- impala与hive的比较以及impala的有缺点
- 使用定时器通过改变图片的src来切换图片
- 两个int(32位)整数m和n的二进制表达中,有多少个位(bit)不同
- httpd 不带反斜杠 出现 301重定向