Base64 工具类
来源:互联网 发布:英译汉用哪个软件好 编辑:程序博客网 时间:2024/04/28 01:46
package com.hisun.itoppay.atc;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.bouncycastle.util.encoders.Base64;
public class Base64Utils
{
private static final int CACHE_SIZE = 1024;
public static byte[] decode(String base64)
throws Exception
{
return Base64.decode(base64.getBytes());
}
public static String encode(byte[] bytes)
throws Exception
{
return new String(Base64.encode(bytes));
}
public static String encodeFile(String filePath)
throws Exception
{
byte[] bytes = fileToByte(filePath);
return encode(bytes);
}
public static void decodeToFile(String filePath, String base64)
throws Exception
{
byte[] bytes = decode(base64);
byteArrayToFile(bytes, filePath);
}
public static byte[] fileToByte(String filePath)
throws Exception
{
byte[] data = new byte[0];
File file = new File(filePath);
if (file.exists()) {
FileInputStream in = new FileInputStream(file);
ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
byte[] cache = new byte[1024];
int nRead = 0;
while ((nRead = in.read(cache)) != -1) {
out.write(cache, 0, nRead);
out.flush();
}
out.close();
in.close();
data = out.toByteArray();
}
return data;
}
public static void byteArrayToFile(byte[] bytes, String filePath)
throws Exception
{
InputStream in = new ByteArrayInputStream(bytes);
File destFile = new File(filePath);
if (!destFile.getParentFile().exists()) {
destFile.getParentFile().mkdirs();
}
destFile.createNewFile();
OutputStream out = new FileOutputStream(destFile);
byte[] cache = new byte[1024];
int nRead = 0;
while ((nRead = in.read(cache)) != -1) {
out.write(cache, 0, nRead);
out.flush();
}
out.close();
in.close();
}
}
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.bouncycastle.util.encoders.Base64;
public class Base64Utils
{
private static final int CACHE_SIZE = 1024;
public static byte[] decode(String base64)
throws Exception
{
return Base64.decode(base64.getBytes());
}
public static String encode(byte[] bytes)
throws Exception
{
return new String(Base64.encode(bytes));
}
public static String encodeFile(String filePath)
throws Exception
{
byte[] bytes = fileToByte(filePath);
return encode(bytes);
}
public static void decodeToFile(String filePath, String base64)
throws Exception
{
byte[] bytes = decode(base64);
byteArrayToFile(bytes, filePath);
}
public static byte[] fileToByte(String filePath)
throws Exception
{
byte[] data = new byte[0];
File file = new File(filePath);
if (file.exists()) {
FileInputStream in = new FileInputStream(file);
ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
byte[] cache = new byte[1024];
int nRead = 0;
while ((nRead = in.read(cache)) != -1) {
out.write(cache, 0, nRead);
out.flush();
}
out.close();
in.close();
data = out.toByteArray();
}
return data;
}
public static void byteArrayToFile(byte[] bytes, String filePath)
throws Exception
{
InputStream in = new ByteArrayInputStream(bytes);
File destFile = new File(filePath);
if (!destFile.getParentFile().exists()) {
destFile.getParentFile().mkdirs();
}
destFile.createNewFile();
OutputStream out = new FileOutputStream(destFile);
byte[] cache = new byte[1024];
int nRead = 0;
while ((nRead = in.read(cache)) != -1) {
out.write(cache, 0, nRead);
out.flush();
}
out.close();
in.close();
}
}
阅读全文
0 0
- Base64 工具类
- Base64工具类
- BASE64编码工具类
- 工具类 Base64封装
- Base64 工具类
- Java-Base64工具类
- Base64工具类
- Base64工具类
- Base64工具类,Base64Utils
- Android Base64编码工具类
- base64转码工具类
- 工具类:Base64快速加密
- base64加密解密工具类
- Base64加密解密工具类
- Base64图片工具类-JAVA
- Base64 编解码 工具类
- 关于Base64工具类并发问题
- Java BASE64编码解码工具类
- R语言入门--R中的factor
- 收藏一些好用的网址,也欢迎大家一起提供,我还整理
- spark--transform算子--sample
- 设计模式(17)-中介者模式
- 说说JSON和JSONP,也许你会豁然开朗
- Base64 工具类
- Hadoop Eclipse插件的使用(笔记6)
- JS中URL参数的编解码
- R语言学习-查看数据头和尾
- hibernate的三种状态
- Java 集合系列13之 WeakHashMap源码解析
- HTTP状态码302、303和307
- Hex 和string 互转工具类
- 一个故事讲完https