java 将本地文件或网络文件与base64互相转换
一:将网络文件转为Base64
将文件转为base64
public static String fileToBase64(String url){
int byteread = 0;
String total = null;
byte[] totalbyte = new byte[0];
try {
URL url = new URL(url);
URLConnection conn = url.openConnection();
InputStream inStream = conn.getInputStream();
byte[] buffer = new byte[1204];
while ((byteread = inStream.read(buffer)) != -1) {
//拼接流,这样写是保证文件不会被篡改
totalbyte = byteMerger(totalbyte,buffer,byteread);
}
inStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return Base64.encodeBase64String(totalbyte)
}
将base转为文件
public static void base64ToFile(String base64, String filePath) {
try {
byte[] bytes = Base64.decodeBase64(base64);
//base解密
File videoFile = new File(filePath);
//输入文件
FileOutputStream fos = new FileOutputStream(videoFile);
fos.write(bytes, 0, bytes.length);
fos.flush();
fos.close();
} catch (IOException e) {
}
}
二:将本地文件转Base64
转Base64
public static String videoToBase64(File videofilePath) {
long size = videofilePath.length();
byte[] imageByte = new byte[(int) size];
FileInputStream fs = null;
BufferedInputStream bis = null;
try {
fs = new FileInputStream(videofilePath);
bis = new BufferedInputStream(fs);
bis.read(imageByte);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fs != null) {
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return Base64.encodeBase64String(imageByte);
}
将Base64转文件
public static void base64ToFile(String base64, String filePath) {
try {
byte[] bytes = Base64.decodeBase64(base64);
//base解密
File videoFile = new File(filePath);
//输入文件
FileOutputStream fos = new FileOutputStream(videoFile);
fos.write(bytes, 0, bytes.length);
fos.flush();
fos.close();
} catch (IOException e) {
}
}
注意:
在将文件转Base64字符时,如果使用sun下的BASE64Encoder时会导致转换出来的Base64自动换行,原因是RFC2045中有规定Base64一行不能超过76字符,超过则添加回车换行符所以导致转换出来的Base64字符会出现换行,解决方法是使用Apache的 commons-codec.jar,Base64.encodeBase64String(byte[])得到的Base64字符不会出现换行
commons-codec 1.4版本时也会出现换行,使用1.8时不会出现换行,其他版本没有测试
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.8</version>
</dependency>
java 将本地文件或网络文件与base64互相转换的更多相关文章
- C#判断本地文件,网络文件是否存在是否存在
File.Exists 方法 (String) 确定指定的文件是否存在. 命名空间: System.IO程序集: mscorlib(位于 mscorlib.dll) 参数 path Type: ...
- JAVA多线程下载网络文件
JAVA多线程下载网络文件,开启多个线程,同时下载网络文件. 源码如下:(点击下载 MultiThreadDownload.java) import java.io.InputStream; im ...
- java 下载网络文件
1.FileUtils.copyURLToFile实现: import java.io.File; import java.net.URL; import org.apache.commons.io. ...
- Java读取并下载网络文件
CreateTime--2017年8月21日10:11:07 Author:Marydon import java.io.ByteArrayOutputStream; import java.io ...
- Java读取本地json文件
背景 之前一直在弄一个Java爬虫,将爬取的信息保存到了数据库中.但这毕竟是一个课程设计,在设计前端GUI,展示数据的时候最开始是直接通过select语句从数据库中查找的,但我担心交给老师后,老师还要 ...
- ftp 根据特定正则匹配文件名 下载到本地 并且上传文件到ftp java *** 最爱那水货
/** * 建立FTP链接,FTP服务器地址.端口.登陆用户信息都在配置里配置即可. * @throws IOException */ public boolean connectFtp(String ...
- 使用AVFoundation仅仅生成缩略图,不进行播放视频(本地和网络文件都可以创建视频缩略图)
使用MPMoviePlayerController来生成缩略图足够简单,但是如果仅仅是是为了生成缩略图而不进行视频播放的话,此刻使用 MPMoviePlayerController就有点大材小用了.其 ...
- java的本地文件操作
一.文件的创建.删除和重命名 File file = new File("/bin/hello.txt");//文件无法被创建,系统找不到指定的路径file.createNewFi ...
- Hadoop HDFS (3) JAVA訪问HDFS之二 文件分布式读写策略
先把上节未完毕的部分补全,再剖析一下HDFS读写文件的内部原理 列举文件 FileSystem(org.apache.hadoop.fs.FileSystem)的listStatus()方法能够列出一 ...
随机推荐
- 在虚拟机中安装Mysql
目录 下载Mysql 安装 配置mysql允许远程访问 下载Mysql 下载地址:http://dev.mysql.com/downloads/mysql 我这里下载的是安装版本 安装 配置mysql ...
- SpringBoot---关于 WebMvcConfigurerAdapter 过时问题及解决方法
SpringBoot---关于 WebMvcConfigurerAdapter 过时问题及解决方法 环境: IDEA :2020.1 Maven:3.5.6 SpringBoot: 2.3.2 在Sp ...
- Android 使用Zxing报错:Channel is unrecoverably broken and will be disposed!
使用Zxing的扫描二维码库,修改成从相册识别二维码图片,根据网上的demo修改,继而在我使用的fragment报错Channel is unrecoverably broken and will b ...
- 对java程序员来说时间格式永远让人挠头来看Java Date Time 教程-时间测量
在Java中,用System.currentTimeMillis()来测量时间最方便. 你要做的是在某些操作之前获取到时间,然后在这些操作之后你想要测量时间,算出时间差.下面是一个例子: long s ...
- mycli工具mysql命令自动补全
简介 MyCli 是一个 MySQL 的命令行客户端,可以实现自动补全和语法高亮.MyCli 也可用于 MariaDB 和Percona. 项目地址:http://mycli.net/ 安装 pip安 ...
- golang gorm 基本使用
原文链接:golang orm 框架之 gorm gorm 用法介绍 库安装 go get -u github.com/jinzhu/gorm 数据库连接 import ( "github. ...
- ubuntu 构建 deb 安装包
源码包下载:http://mirrors.163.com/ubuntu/ 编译工具安装: apt-get install dpkg-dev 以openvswitch为例: wget http://mi ...
- linux tmpfs及消耗内存脚本
一.tmpfs介绍 tmpfs是一种虚拟内存文件系统,正如这个定义它最大的特点就是它的存储空间在VM里面VM是由linux内核里面的vm子系统管理的东西,现在大多数操作系统都采用了虚拟内存管理机制VM ...
- JAVA作业—字符串操作
------------恢复内容开始------------ ------------恢复内容开始------------ ------------恢复内容开始------------ ------- ...
- Federated Learning with Matched Averaging
挖个坑吧,督促自己仔细看一遍论文(ICLR 2020),看看自己什么时候也能中上那么一篇(流口水)~ 郑重声明:原文参见标题,如有侵权,请联系作者,将会撤销发布! Abstract 联邦学习允许边缘设 ...