首页 技术 正文
技术 2022年11月14日
0 收藏 701 点赞 3,162 浏览 2638 个字

通过form表单上传图片时,有时候web容器对文件大小的限制会影响我们上传。这时,前端页面可以考虑将图片转换成base64串来实现上传。

图片与Base64的互转,其实就是利用了文件流与Base64的互转。

文件转换成Base64字符串:读取文件的输入流,因为文件流是字节流,所以要放到byte数组(字节数组,byte取值范围-128~127)里,然后对byte数组做Base64编码,返回字符串。

Base64串转换成文件:对Base64编码的字符串进行Base64解码,得到byte数组,利用文件输出流将byte数据写入到文件。

Talk is cheap, show me the code。直接上代码:

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;import java.io.*;public class ImageBase64Converter {
/**
* 本地文件(图片、excel等)转换成Base64字符串
*
* @param imgPath     
*/
public static String convertFileToBase64(String imgPath) {
byte[] data = null;
// 读取图片字节数组
try {
InputStream in = new FileInputStream(imgPath);
System.out.println("文件大小(字节)="+in.available());
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
// 对字节数组进行Base64编码,得到Base64编码的字符串
BASE64Encoder encoder = new BASE64Encoder();
String base64Str = encoder.encode(data);
return base64Str;
} /**
* 将base64字符串,生成文件
*/
public static File convertBase64ToFile(String fileBase64String, String filePath, String fileName) { BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try {
File dir = new File(filePath);
if (!dir.exists() && dir.isDirectory()) {//判断文件目录是否存在
dir.mkdirs();
} BASE64Decoder decoder = new BASE64Decoder();
byte[] bfile = decoder.decodeBuffer(fileBase64String); file = new File(filePath + File.separator + fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bfile);
return file;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}}

test:

public static void main(String[] args) {
long start = System.currentTimeMillis();
String imgBase64Str= ImageBase64Converter.convertFileToBase64("D:\\Pictures\\科技\\liziqi-李子柒爆红.jpg");
// System.out.println("本地图片转换Base64:" + imgBase64Str);
System.out.println("Base64字符串length="+imgBase64Str.length());
ImageBase64Converter.convertBase64ToFile(imgBase64Str,"D:\\Pictures\\科技","test.jpg");
System.out.println("duration:"+(System.currentTimeMillis()-start)); start=System.currentTimeMillis();
String fileBase64Str= ImageBase64Converter.convertFileToBase64("D:\\Pictures\\科技\\PayOrderList200109075516581.xlsx");
// System.out.println("本地excel转换Base64:" + fileBase64Str);
System.out.println("size="+fileBase64Str.length());
ImageBase64Converter.convertBase64ToFile(fileBase64Str,"D:\\Pictures\\科技","test.xlsx");
System.out.println("duration:"+(System.currentTimeMillis()-start)); }

执行结果:

文件大小(字节)=2820811
Base64字符串length=3860058
duration:244
文件大小(字节)=25506
size=34902
duration:10

提醒一下:获取文件的大小是用FileInputStream实例的available()方法哦,用File实例的length()返回的是0。

如下图,测试方法里图片文件“liziqi-李子柒爆红.jpg”的大小正是2820811字节   ÷1024=2755KB  ÷1024=2.68M

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,910
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,435
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,250
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,061
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,693
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,731