Java中如何将字节数组转换为图片呢?

陈欣 Java经验 发布时间:2023-08-08 10:58:49 阅读数:7242 1
下文笔者讲述字节数组转换为图片的方法及示例分享,如下所示

字节转换为图片的实现思路

字节数组转换为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();
		 }
	 }
  }
}
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202308/16914635547168.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者