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;

}

你可能感兴趣的:(byte和输入流,byte和字符串之间的各种转换)