//将字符串转换成byte数组
public static byte[] fileToBytes(String filePath) {
byte[] buffer = null;
File file = new File(filePath);
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
try {
fis = new FileInputStream(file);
bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
buffer = bos.toByteArray();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
if (null != bos) {
bos.close();
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
if (null != fis) {
fis.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
return buffer;
}
posted on 2020-05-26 11:13  文种玉  阅读(158)  评论(0编辑  收藏  举报