公司的做网站,深圳龙岗属于什么风险,深圳互联网科技有限公司,版权申请网站Java中#xff0c;将字节数组转成图片的有很多种方式#xff0c;今天在这里记录其中一种#xff0c;方便以后查询#xff0c;也可以提供给没有接触的童鞋做一个参考。首先是将图片转成字节数组import sun.misc.BASE64Encoder;import java.io.*;// 传入图片路径#xff0c;…Java中将字节数组转成图片的有很多种方式今天在这里记录其中一种方便以后查询也可以提供给没有接触的童鞋做一个参考。首先是将图片转成字节数组import sun.misc.BASE64Encoder;import java.io.*;// 传入图片路径获取图片FileInputStream fis new FileInputStream(/Users/curry/error.png);BufferedInputStream bis new BufferedInputStream(fis);ByteArrayOutputStream bos new ByteArrayOutputStream();byte[] buff new byte[1024];int len 0;while ((len fis.read(buff)) ! -1) {bos.write(buff, 0, len);}// 得到图片的字节数组byte[] result bos.toByteArray();// 将数组转为字符串BASE64Encoder encoder new BASE64Encoder();String str encoder.encode(result).trim();将数组转为图片import sun.misc.BASE64Decoder;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;BASE64Decoder decoder new BASE64Decoder();byte[] imgbyte decoder.decodeBuffer(刚刚将字节数组转成的字符串);OutputStream os new FileOutputStream(/Users/curry/text.png);os.write(imgbyte, 0, imgbyte.length);os.flush();os.close();补充知识java将图片转化为base64和base64转化为图片编码并保存在本地我就废话不多说了大家还是直接看代码吧~public class Base64Convert {/*** Description 图片转化成base64字符串* param: path* Return:*/public static String GetImageStr(String path){//将图片文件转化为字节数组字符串并对其进行Base64编码处理//待处理的图片String imgFile path;InputStream in null;byte[] data null;//读取图片字节数组try{in new FileInputStream(imgFile);data new byte[in.available()];in.read(data);in.close();}catch (IOException e){e.printStackTrace();}//对字节数组Base64编码BASE64Encoder encoder new BASE64Encoder();//返回Base64编码过的字节数组字符串return encoder.encode(data);}/*** Description base64字符串转化成图片* param: imgStr* Return:*/public static boolean GenerateImage(String imgStr,String photoname){//对字节数组字符串进行Base64解码并生成图片//图像数据为空if (imgStr null)return false;BASE64Decoder decoder new BASE64Decoder();try{//Base64解码byte[] b decoder.decodeBuffer(imgStr);for(int i0;i{if(b[i]0){//调整异常数据b[i]256;}}//生成jpeg图片String imagePath Config.getUploadPhysicalPath();//System.currentTimeMillis()//新生成的图片String imgFilePath imagePathphotoname;OutputStream out new FileOutputStream(imgFilePath);out.write(b);out.flush();out.close();return true;}catch (Exception e){return false;}}}以上这篇Java将byte[]转图片存储到本地的案例就是小编分享给大家的全部内容了希望能给大家一个参考也希望大家多多支持自学编程网。