现在的位置: 首页 > 综合 > 正文

android 文件上传时将file转化为byte[]

2017年01月08日 ⁄ 综合 ⁄ 共 450字 ⁄ 字号 评论关闭

 

public static byte[] File2Bytes(File file) {
int byte_size = 1024;
byte[] b = new byte[byte_size];
try {
FileInputStream fileInputStream = new FileInputStream(file);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(
byte_size);
for (int length; (length = fileInputStream.read(b)) != -1;) {
outputStream.write(b, 0, length);
}
fileInputStream.close();
outputStream.close();
return outputStream.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
   现在就可以将byte[]上传了。

抱歉!评论已关闭.