赞
踩
使用到工具类依赖
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
通过FileUtils.openInputStream()
将视频文件转化成输入流。
然后通过IOUtils.toByteArray()
直接将流经过Base64编码为String。最后通过FileWriter
直接将String写入即可。
@Test
public void test() throws IOException {
String sourceVideo = "/Users/LiuShihao/IdeaProjects/as-video-call-bussiness/as-video-business/src/main/resources/static/5.3gp" ;
InputStream inputStream = FileUtils.openInputStream(new File(sourceVideo));
// encode
String ss = new String(Base64.getEncoder().encode(IOUtils.toByteArray(inputStream)), Charsets.ISO_8859_1);
File txtFilePath = new File("/Users/LiuShihao/IdeaProjects/as-video-call-bussiness/as-video-business/src/main/resources/static/5.3.txt");
FileWriter fileWriter = new FileWriter(txtFilePath);
fileWriter.write(ss);
fileWriter.flush();
fileWriter.close();
}
使用BufferReader
直接读取txt信息。一行行的读取到一个StringBuffer中,转成String,在经过Base64解码。最后通过字节流写成一个新的mp4文件。
@Test
public void test2() throws IOException {
File txtFilePath = new File("/Users/LiuShihao/IdeaProjects/as-video-call-bussiness/as-video-business/src/main/resources/static/1.3.txt");
BufferedReader bufferedReader = new BufferedReader(new FileReader(txtFilePath));
StringBuffer sbf = new StringBuffer();
String tempStr;
while ((tempStr = bufferedReader.readLine()) != null) {
sbf.append(tempStr);
}
bufferedReader.close();
String ss = sbf.toString();
InputStream is = new ByteArrayInputStream(Base64.getDecoder().decode(ss.getBytes(Charsets.ISO_8859_1)));
FileOutputStream fos = new FileOutputStream(new File("/Users/LiuShihao/IdeaProjects/as-video-call-bussiness/as-video-business/src/main/resources/static/1.3gp_copy.mp4"));
byte[] bytes = new byte[is.available()];
System.out.println(bytes.length);
int x = -1;
while((x = is.read(bytes)) != -1) {
fos.write(bytes);
}
is.close();
fos.close();
}
public ResponseData imageToBase64(@RequestParam(name = "file") MultipartFile[] imgs){
log.info("文件长度:"+imgs.length);
ResponseData<Object> data = ResponseData.defaultSuccess();
HashMap<String, Object> map = new HashMap<>();
try {
for (MultipartFile img : imgs) {
log.info("文件名:"+img.getOriginalFilename());
InputStream inputStream = img.getInputStream();
String base64 = new String(Base64.getEncoder().encode(IOUtils.toByteArray(inputStream)), Charsets.UTF_8);
map.put(img.getOriginalFilename(),base64);
}
data.setData(map);
}catch (Exception e){
data = ResponseData.defaultFail();
data.setMessage(e.getMessage());
}
return data;
}
入参为MultipartFile
类型,接收图片,在通过getInputStream
转输入流
,通过IOUtils.toByteArray
方法将输入流
转为ByteArray
,在经过Base64.getEncoder().encode()
进行Base64编码返回即可。
public ResponseData base64ToImage(@RequestBody Map<String,String> map ){
ResponseData<Object> data = ResponseData.defaultSuccess();
try {
String base64 = map.get("base64");
ByteArrayInputStream in = new ByteArrayInputStream(Base64.getDecoder().decode(base64.getBytes(Charsets.UTF_8)));
File file = new File("/Users/LiuShihao/IdeaProjects/besttone-call-business/1.jpeg");
FileOutputStream fos = new FileOutputStream(file);
byte[] bytes = new byte[in.available()];
int x = -1;
while((x = in.read(bytes)) != -1) {
fos.write(bytes);
}
in.close();
fos.close();
data.setData("SUCCESS");
}catch (Exception e){
data = ResponseData.defaultFail();
data.setMessage(e.getMessage());
}
return data;
}
入参为经过Base64编码后的字符,首先通过Base64.getDecoder().decode()
将字符串解码,在通过new
构造一个ByteArrayInputStream
输入流,构造一个File
,获取这个File
的输出流FileOutputStream
,read
读取输入流。write
写入输出流。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。