java blob转base64_JAVA 文件与base64之间的转化, 以及Web实现base64上传文件

<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;

/**

* 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("上传失败");

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值