byte转inputStream及互相转化和各种的文件转化字符串转图片byte转字符串各种的方法

1、将File、FileInputStream 转换为byte数组:
File file = new File("file.txt");
InputStream input = new FileInputStream(file);
byte[] byt = new byte[input.available()];
input.read(byt);
2、将byte数组转换为InputStream:
    byte[] byt = new byte[1024];
    InputStream input = new ByteArrayInputStream(byt);
 
3、将byte数组转换为File:
File file = new File('');
OutputStream output = new FileOutputStream(file);
BufferedOutputStream bufferedOutput = new BufferedOutputStream(output);
bufferedOutput.write(byt);

 

将文件转成base64 字符串 

public static String encodeBase64File(String path) throws Exception {  

  File file = new File(path);;  

  FileInputStream inputFile = new FileInputStream(file);  

  byte[] buffer = new byte[(int) file.length()];  

  inputFile.read(buffer);  

  inputFile.close();  

  return new BASE64Encoder().encode(buffer);  

  

 }  

将base64字符解码保存文件 

public static void decoderBase64File(String base64Code, String targetPath)  

   throws Exception {  

  byte[] buffer = new BASE64Decoder().decodeBuffer(base64Code);  

 FileOutputStream out = new FileOutputStream(targetPath);  

  out.write(buffer);  

  out.close();  

   }  

   将base64字符保存文本文件 

   public static void toFile(String base64Code, String targetPath)  

   throws Exception {  

byte[] buffer = base64Code.getBytes();  

  FileOutputStream out = new FileOutputStream(targetPath);  

  out.write(buffer);  

  out.close();  

 }  

  public static void main(String[] args) {  

  try {  

   String base64Code = encodeBase64File("D:/0101-2011-qqqq.tif");  

   System.out.println(base64Code);  

   decoderBase64File(base64Code, "D:/2.tif");  

  toFile(base64Code, "D:\\three.txt");  

  } catch (Exception e) {  

   e.printStackTrace();  

    }  

  }  

Base64字符串转图片
public static BufferedImage base64ToImage(String base64Str)throws Exception{

byte[] bytes = Base64Utils.base64ToBytes(BaseUtils.replaceBlank(base64Str));

InputStream inputStream = new ByteArrayInputStream(bytes);

BufferedImage image = ImageIO.read(inputStream);

inputStream.close();

return image;

}

Base64字符串转图片

public static OutputStream base64ToStream(String base64Str,OutputStream outputStream) throws Exception{

byte[] bytes = Base64Utils.base64ToBytes(BaseUtils.replaceBlank(base64Str));

outputStream.write(bytes);  

return outputStream;

}

Base64字符串转图片

public static byte[] base64ToBytes(String base64Str) throws Exception{

if(StringUtils.isEmpty(base64Str)){

throw new IllegalArgumentException("Base64字符串不能为空");

}

BASE64Decoder decoder = new BASE64Decoder();

//Base64解码

byte[] b = decoder.decodeBuffer(BaseUtils.replaceBlank(base64Str));

return b;

}

输入流转byte[]再转Base64字符串

 public static String stream2Base64(InputStream inputStream)throws Exception{

ByteArrayOutputStream outStream = new ByteArrayOutputStream();

byte[] data = new byte[1024];

int count = -1;

while ((count = inputStream.read(data, 0, 1024)) != -1)

outStream.write(data, 0, count);

data = null;

byte[] a = outStream.toByteArray();

// 对字节数组Base64编码

BASE64Encoder encoder = new BASE64Encoder();

return encoder.encode(a);// 返回Base64编码过的字节数组字符串

}

输入流转Base64字符串

public static String streamToBase64(InputStream inputStream)throws Exception{

byte[] data = new byte[inputStream.available()];

// 对字节数组Base64编码

BASE64Encoder encoder = new BASE64Encoder();

return encoder.encode(data);// 返回Base64编码过的字节数组字符串

}

Base64转文件

public static void toFile(String base64,String filePath) throws IOException{

byte[] decodeBuffer = new BASE64Decoder().decodeBuffer(BaseUtils.replaceBlank(base64));

FileOutputStream out = new FileOutputStream(filePath);

out.write(decodeBuffer);

out.flush();

out.close();

}

文件转Base64

public static String toBase64(File file) throws IOException{

FileInputStream inputFile  = new FileInputStream(file);

byte[] buffer = new byte[(int) file.length()];

inputFile.read(buffer);

inputFile.close();

String base64 = new BASE64Encoder().encode(buffer);

return base64;

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值