首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 互联网 >

从互联网络(路径)将图片转化为byte数组

2013-01-07 
从互联网(路径)将图片转化为byte数组public byte[] getBytes(String filePath) {try {InputStream in nu

从互联网(路径)将图片转化为byte数组
public byte[] getBytes(String filePath) {
try {
InputStream in = null;
DataInputStream dis = null;
HttpURLConnection connection = null;
URL server = new URL(filePath);
connection = (HttpURLConnection) server.openConnection();
connection.connect();
in = connection.getInputStream();
dis = new DataInputStream(in);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int ch = 0;
while ((ch = dis.read()) != -1) {
baos.write(ch);
}
byte[] imageData = baos.toByteArray();
if (baos != null) {
baos.close();
baos = null;
}
if (dis != null) {
dis.close();
dis = null;
}
if (in != null) {
in.close();
in = null;
}
if (connection != null) {
connection.disconnect();
connection = null;
}
return imageData;
} catch (Exception e) {
e.printStackTrace();
}
return null;
} 1 楼 tang&qiang 2010-11-30   (新手)能给点说明吗???这样看着头晕的很...

热点排行