Java中如何将字节数组转换为图片呢?
下文笔者讲述字节数组转换为图片的方法及示例分享,如下所示
字节转换为图片的实现思路
字节数组转换为ByteArrayInputStream ByteArrayInputStream转换为文件FileOutStream例:字节数组转图片的示例
public static void byteArrayToFile(byte[] src,String filePath) { //1.创建源 File file = new File(filePath); //选择流 InputStream writing = null; OutputStream os = null; try { writing = new ByteArrayInputStream(src); os = new FileOutputStream(file); byte[] frush = new byte[5];//3表示0个字节为一段 int len = -1; while((len=writing.read(frush))!=-1) { os.write(frush,0,len); } os.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(os!=null) { try { os.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。