https文件路径
public byte[] getFile(String url) throws IOException {
URL urlConet = new URL(url);
HttpURLConnection con = (HttpURLConnection)urlConet.openConnection();
con.setRequestMethod(“GET”);
con.setConnectTimeout(4 * 1000);
InputStream inStream = con .getInputStream();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[2048];
int len = 0;
while( (len=inStream.read(buffer)) != -1 ){
outStream.write(buffer, 0, len);
}
inStream.close();
byte[] data = outStream.toByteArray();
return data;
}
本地文件路径
public byte[] image2Bytes(String imgSrc)
{
FileInputStream fin;
byte[] bytes = null;
try {
fin = new FileInputStream(new File(imgSrc));
bytes = new byte[fin.available()];
//将文件内容写入字节数组
fin.read(bytes);
fin.close();
} catch (Exception e) {
e.printStackTrace();
}
return bytes;
}