/**
* 文件转换成字节数组
* @param filePath
* @return
*/
public static byte[] fileToByteArray(String filePath) {
File file = new File(filePath);
byte[] dest = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
baos = new ByteArrayOutputStream();
is = new FileInputStream(file);
byte[] flush = new byte[1024];
int len = -1;
while ((len = is.read(flush)) != -1) {
baos.write(flush, 0, len);
}
baos.flush();
return baos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
/**
* 字节数组转换成文件
* @param filePath
* @return
*/
public static void byteArrayToFile(byte[] src, String filePath) {
File dest = new File(filePath);
InputStream is = null;
OutputStream os = null;
try {
is = new ByteArrayInputStream(src);
os = new FileOutputStream(dest);
byte[] flush = new byte[5];
int len = -1;
while ((len = is.read(flush)) != -1) {
os.write(flush, 0, len);
}
os.flush();
} catch (Exception e) {
e.printStackTrace();
}finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。