Java - image | File

2021-04-03 22:29

阅读:314

标签:trace   string   null   available   size   get   rac   ace   nts   

image

读写本地图片

public static String saveImgaeToLocal(String path, byte[] imgdata) throws IOException {
  OutputStream os = null;
  try {
      os = new FileOutputStream(path);
      os.write(imgdata, 0, imgdata.length);
      os.flush();
  } catch (Exception e) { return e.getMessage(); } finally {
    if (os != null) { os.close(); }
  }
  return "";
}
public static String readImageFromLocal(String path) {
  String data = "";
  DataInputStream dataInputStream = null;
  try {
    dataInputStream = new DataInputStream(new FileInputStream(path));
    byte[] b = new byte[dataInputStream.available()];//创建一个字节数组,长度等于二进制图片的返回的实际字节数
    dataInputStream.read(b);//读取图片信息放入这个b数组
    return Base64Util.encode(b);
  } catch (IOException e) { e.printStackTrace(); } finally {
    try {
      if (null != dataInputStream) { dataInputStream.close();//关闭流 }
    } catch (IOException e) { }
  }
}

file

读写本地文件


Java - image | File

标签:trace   string   null   available   size   get   rac   ace   nts   

原文地址:https://www.cnblogs.com/wjcx-sqh/p/13439097.html


评论


亲,登录后才可以留言!