byte数组到图片到硬盘上 与对象转byte数组 方法

/**
* 对象转数组
* @param obj
* @return
*/
public byte[] toByteArray(Object obj) {
byte[] bytes = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
oos.flush();
bytes = bos.toByteArray();
oos.close();
bos.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return bytes;
}

//byte数组到图片到硬盘上
public void byte2image(byte[] data, String name) {
String path = "d:\\" + name + ".png";
if (data.length < 3 || path.equals(""))
return;//判断输入的byte是否为空
try {
FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));//打开输入流
imageOutput.write(data, 0, data.length);//将byte写入硬盘
imageOutput.close();
System.out.println("Make Picture success,Please find image in " + path);
} catch (Exception ex) {
System.out.println("Exception: " + ex);
ex.printStackTrace();
}
}

posted @ 2017-03-14 10:22  Kevin_Zhou_9  阅读(435)  评论(0编辑  收藏  举报