import java.io.FileInputStream;
import java.io.FileOutputStream;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
* base64 与 file 之间的相互转化
* 实现形式, 懒汉式的单例模式
public class Base64UploadClass {
// 私有化构造器
private Base64UploadClass() {
// 事先定义一个变量存放该类的实例
private static Base64UploadClass fileBase64 = null;
// 对外暴露一个静态方法获取该类的实例
public static Base64UploadClass getFileBase64() {
if (fileBase64 == null) {
fileBase64 = new Base64UploadClass();
return fileBase64;
// 将 file 转化为 Base64
public String fileToBase64(String path) {
File file = new File(path);
FileInputStream inputFile;
try {
inputFile = new FileInputStream(file);
byte[] buffer = new byte[(int) file.length()];
inputFile.read(buffer);
inputFile.close();
return new BASE64Encoder().encode(buffer);
} catch (Exception e) {
throw new RuntimeException("文件路径无效\n" + e.getMessage());
// 将 base64 转化为 file
public boolean base64ToFile(String base64, String path) {
byte[] buffer;
try {
buffer = new BASE64Decoder().decodeBuffer(base64);
FileOutputStream out = new FileOutputStream(path);
out.write(buffer);
out.close();
return true;
} catch (Exception e) {
throw new RuntimeException("base64字符串异常或地址异常\n" + e.getMessage());
<2> servlet 借助 base64 实现文件上传
package servlet_file_upload;
import java.io.IOException;
import java.net.URLDecoder;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/Base64UploadServlet")
public class Base64UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public Base64UploadServlet() {
super();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 允许跨域访问,并设置请求编码和输出编码为 UTF-8
response.addHeader("Access-Control-Allow-Origin", "*");
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
// 获取文件将要保存到的文件夹路径
String path = getServletContext().getRealPath("");
// 接收base64文件字符串, 并对文件字符串进行解码
String fileContent = request.getParameter("file");
fileContent = URLDecoder.decode(fileContent, "UTF-8");
// 获取文件保存的相对路径
String returnPath = "Upload/" + System.currentTimeMillis() + "." + fileContent.split("\\.")[1];
// 保存文件返回路径
Base64UploadClass fileBase64 = Base64UploadClass.getFileBase64();
if(fileBase64.base64ToFile(fileContent.split("\\.")[0], path + returnPath)){
response.getWriter().write(returnPath);
} else {
response.getWriter().write("上传失败");
<1>文件与base64字符串之间的转化package servlet_file_upload;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;/**...
1.
Base64
to
Blob
function dataURLto
Blob
(dataurl) {
var arr = dataurl.split(','), //分割为数组,分割到第一个逗号
let mime = arr[0].match(/:(.*?);/)[1],//获取分割后的
base64
前缀中的类型
let bstr = window.atob(arr[1]),
let n = bstr.length,
let u8arr = new Uint8Arr
在
Java
Script 中,可以使用 `atob()` 函数将
base64
编码的字符串
转
换为二进制数据,然后使用 `
Blob
()` 构造函数将其
转
换为
Blob
对象。
```
java
script
//
base64
转
Blob
function
base64
To
Blob
(
base64
) {
let binary = atob(
base64
);
let array = [];
for (let i = 0; i < binary.length; i++) {
array.push(binary.charCodeAt(i));
return new
Blob
([new Uint8Array(array)], { type: "image/png" });
使用的时候直接调用函数,就能得到一个
Blob
对象了。
```
java
script
let
base64
= "iVBORw0KGg....";
let
blob
=
base64
To
Blob
(
base64
);
需要注意,这个例子中使用了 "image/png" 作为
Blob
对象的类型,如果是其他类型的图片需要改成相应的类型。