File 文件 byte[] 字节 Base64 互转
来源:互联网 发布:csgo低配置优化补丁 编辑:程序博客网 时间:2024/06/05 11:49
/** * 通过文件路径将文件转成Base64编码 * @param path 文件路径 * @return base64结果 */ public static String imageToBase64(String path) { // 将图片文件转化为字节数组字符串,并对其进行Base64编码处理 byte[] data = null; // 读取图片字节数组 try { InputStream in = new FileInputStream(path); data = new byte[in.available()]; in.read(data); in.close(); } catch (IOException e) { e.printStackTrace(); } // 对字节数组Base64编码 BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(data);// 返回Base64编码过的字节数组字符串 } /** * 将图片文件转成Base64编码 * @param file 文件 * @return base64码 */ public static imageToBase64(File file) { // 将图片文件转化为字节数组字符串,并对其进行Base64编码处理 byte[] data = null; // 读取图片字节数组 try { InputStream in = new FileInputStream(file); data = new byte[in.available()]; in.read(data); in.close(); } catch (IOException e) { e.printStackTrace(); } // 对字节数组Base64编码 BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(data);// 返回Base64编码过的字节数组字符串 } /** * byte[] 字节转Base64编码 * @param bytes 数组字节 * @return base64码 */ public static stringBase64(byte[] bytes){ BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(bytes);// 返回Base64编码过的字节数组字符串 } /** * 文件转byte[] 数组 * @param file 文件 * @return byte[] */ public static byte[] imgToByte(File file) { byte[] data = null; try { InputStream in = new FileInputStream(file); data = new byte[in.available()]; in.read(data); in.close(); } catch (IOException e) { e.printStackTrace(); } return data; } /** * 对字节数组字符串进行Base64解码并生成图片 * @param imgStr base64字符串 * @param imgFilePath 指定输出路径 * @return 布尔值 */public static boolean GenerateImage(String imgStr, String imgFilePath) { if (imgStr == null) // 图像数据为空 return false; BASE64Decoder decoder = new BASE64Decoder(); try { // Base64解码 byte[] bytes = decoder.decodeBuffer(imgStr); for (int i = 0; i < bytes.length; ++i) { if (bytes[i] < 0) {// 调整异常数据 bytes[i] += 256; } } // 生成jpeg图片 OutputStream out = new FileOutputStream(imgFilePath); out.write(bytes); out.flush(); out.close(); return true; } catch (Exception e) { return false; } } /** * 对字节数组字符串进行Base64解码 * @param imgStr base64字符串 * @return byte[] */public static byte[] changeBase64(String imgStr) { String base = ""; BASE64Decoder decoder = new BASE64Decoder(); byte[] bytes = null; try { // Base64解码 bytes = decoder.decodeBuffer(imgStr); for (int i = 0; i < bytes.length; ++i) { if (bytes[i] < 0) {// 调整异常数据 bytes[i] += 256; } } // 生成jpeg图片 } catch (Exception e) { e.printStackTrace(); } return bytes; }
阅读全文
0 0
- File 文件 byte[] 字节 Base64 互转
- 字节转文件 byte-->file
- Java中File文件与Base64互转
- 获取文件字节数组byte[]
- base64转file文件上传
- c#中字节数组byte[]、图片image、流stream,字符串string、内存流MemoryStream、文件file,之间的转换
- C# 文件转字节数组 byte[]
- BASE64 实现string和byte互转
- byte 字节
- base64图片转file文件提交
- java File 与 byte[] 互转
- char byte base64 区别
- 以byte[]类型复制文件 Copy a file with read(byte[] data) and write(byte[] data)
- 文件字节流用base64编码之后,后台接收问题
- java File/IO读写文件 字节流
- 文件与byte[]互转
- java把一个文件转化为byte字节
- Java把一个文件转化为byte字节
- 一步步教你使用云端服务器yeelink远程监控
- mybatis框架入门案例
- shell变量相关记录
- linux ubuntu 16.04下deb文件的安装以及一些问题的解决
- 数字图像处理成长之路7:边缘检测(sobel canny)
- File 文件 byte[] 字节 Base64 互转
- 其他-查看端口和PID以及如何结束进程
- 验证SSL证书是否正确,以及证书所解析出的域名
- 小破文章第一篇
- QT入门注意事项(1)
- YLGameCameraControl
- C++函数特性
- python下安装easy_install、pip遇到的问题
- 随机素数测试和大数分解