后台处理前端64位编码格式的视频以及图片

来源:互联网 发布:mac无法解压zip 错误2 编辑:程序博客网 时间:2024/06/18 08:25

//对前端传来的64位编码视频进行解码和写出

BASE64Decoder decoder = new BASE64Decoder();

String mp4 = imgBase.split(",")[1];//获取视频的64位编码
byte[] b = decoder.decodeBuffer(mp4); // 对mp4进行解码
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 调整异常数据
b[i] += 256;
}
}


Random rnd = new Random();
int a = rnd.nextInt(9999) + 10000;// 生成5位的随机数
String videoPath = CommonPropertiesUtil.getValue("video.properties","videoSrc");//获取配置文件
String path = getRequest().getSession().getServletContext().getRealPath("\\")+ "\\" + videoPath;
// 创建存放视频的文件夹
File f = new File(path);
if (!f.exists()) {
f.mkdirs();
}
String videoFilePath = f + "\\222_" + a + ".mp4"; // 指定视频生成的路径
OutputStream out = new FileOutputStream(videoFilePath); // 新生成的视频
out.write(b);
out.flush();

out.close();


// 对图片进行解码
String photo = img.split(",")[1];//获取前端传来的64位编码图片
byte[] d = decoder.decodeBuffer(photo);
for (int i = 0; i < d.length; ++i) {
if (d[i] < 0) {// 调整异常数据
d[i] += 256;
}
}
String imgPath = CommonPropertiesUtil.getValue("video.properties","imgSrc");
String path1 = getRequest().getSession().getServletContext().getRealPath("\\")+ "\\" + imgPath;
// 创建存放图片的文件夹
File f1 = new File(path1);
if (!f1.exists()) {
f1.mkdirs();
}

String imgFilePath = f1 + "\\222_" + a + ".jpeg";
OutputStream out1 = new FileOutputStream(imgFilePath);// 新生成的图片
out1.write(d);
out1.flush();
out1.close();