安卓端上传至服务器端的代码与Web端上传服务器端代码相比,视频文件处理、截图等操作代码是一致的。
只是前面如何拿到视频文字信息及视频文件有些不同。
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile file = multipartRequest.getFile("file");
String video_details = multipartRequest.getParameter("details");
需要对拿到的json格式字符串进行解析:
List<Video> video_list = OneVideoinformationJsonUtils.OneVideoinformationJsonToList(video_details);
服务器端同样采用SSM框架,用到了注解。
controller层代码如下:
/***********************************视频上传***************************************/
String uploadVideo1 = Contants.targetfolder;//视频最终保存的目录
String uploadVideoImgPath = Contants.imageRealPath;
@RequestMapping("/ToUploadVideo")
public void ToUploadVideo(HttpServletResponse response,HttpServletRequest request) {
response.setCharacterEncoding("UTF-8");
System.out.println("ToUploadVideo>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
String NewVideopath = null;
String filename3 = null;
String imgname = null;
String imgpath =null;
Date day = new Date();
SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String video_time_upload = dft.format(day);
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile file = multipartRequest.getFile("file");
String video_details = multipartRequest.getParameter("details");
System.out.println("details........"+video_details);
List<Video> video_list = OneVideoinformationJsonUtils.OneVideoinformationJsonToList(video_details);
String video_author = video_list.get(0).getVideo_author();
String video_title = video_list.get(0).getVideo_title();
String video_category = video_list.get(0).getVideo_category();
String video_source = video_list.get(0).getVideo_source();
String video_classes = video_list.get(0).getVideo_classes();
String video_money = video_list.get(0).getVideo_money();
String video_introduce = video_list.get(0).getVideo_introduce();
if (file.getSize() != 0) {
//如果上传文件后缀名为MP4,则直接上传至最终文件夹
String path2 = uploadVideo1;
//判断是否创建文件夹
File TempFile = new File(path2);
if (TempFile.exists()) {
if (TempFile.isDirectory()) {
System.out.println("该文件夹存在。");
}else {
System.out.println("同名的文件存在,不能创建文件夹。");
}
}else {
System.out.println("文件夹不存在,创建该文件夹。");
TempFile.mkdir();
}
// 获取上传时候的文件名
String filename = file.getOriginalFilename();
System.out.println("上传时候的文件名为:filename="+filename);
// 获取文件后缀名
String filename_extension = filename.substring(filename
.lastIndexOf(".") + 1);
System.out.println("视频的后缀名:filename_extension="+filename_extension);
//文件名添加时间戳,避免相同文件名出现;同时,时间戳用作视频no
long filename1 = new Date().getTime();
filename = filename.substring(0, filename.lastIndexOf(".")) + "_" + Long.toString(filename1)+"."+filename_extension;
String video_no = "V"+Long.toString(filename1);
System.out.println("视频编号 video_no="+video_no);
System.out.println("添加了时间戳的文件名:filename="+filename);
//去掉后缀的文件名
String filename2 = filename.substring(0, filename.lastIndexOf("."));
System.out.println("去掉后缀的文件名:"+filename2);
//上传到服务器
try {
System.out.println("写入本地磁盘/服务器");
InputStream is = file.getInputStream();
OutputStream os = new FileOutputStream(new File(path2, filename));
int len = 0;
byte[] buffer = new byte[2048];
while ((len = is.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
os.close();
os.flush();
is.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("========视频上传完成=======");
String Mp4path = uploadVideo1;
filename3 = filename2+".mp4";
NewVideopath =Mp4path +filename3;
System.out.println("视频的url:"+NewVideopath);
ConverVideoUtils cv = new ConverVideoUtils();
if (cv.processImg(NewVideopath)) {
System.out.println("截图成功! ");
//获取转码后的mp4截图名
String imgpath0 = uploadVideoImgPath;
imgname = filename2+".png";
imgpath =imgpath0 +imgname;
System.out.println("视频的截图url:"+imgpath);
} else {
System.out.println("截图失败! ");
}
System.out.println("开始进行数据库操作。。。。。。。。。");
Video video = new Video();
//数据库存储信息
video.setVideo_no(video_no);
video.setVideo_author(video_author);
video.setVideo_title(video_title);
video.setVideo_category(video_category);
video.setVideo_classes(video_classes);
video.setVideo_source(video_source);
video.setVideo_money(video_money);
video.setVideo_introduce(video_introduce);
video.setVideo_time_upload(video_time_upload);
video.setVideo_filename(filename3);
video.setVideo_imgname(imgname);
video.setVideo_path(NewVideopath); //已转码后的视频存放地址
video.setVideo_imgpath(imgpath);
// 实现对数据的更新
int n = 0;
n = videoService.saveVideo(video);
try {
PrintWriter out = response.getWriter();
if(n != 0) {
System.out.println("写入数据库成功!");
out.write("true");
}else {
System.out.println("写入数据库失败!");
out.write("false");
}
response.flushBuffer();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
ConverVideoUtils.java文件,给视频截图。
package cn.lijx.mytools;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class ConverVideoUtils {
private String filerealname; //文件名不包括后缀名
private String filename; //包括后缀名
private String imageRealPath = Contants.imageRealPath; // 截图的存放目录
public ConverVideoUtils() {
}
//重构构造方法,传入源视频
public ConverVideoUtils(String path) {
sourceVideoPath = path;
System.out.println("ConverVideoUtils.jsp文件:sourceVideoPath="+sourceVideoPath);
}
//set和get方法传递path
public String getPATH() {
return sourceVideoPath;
}
public void setPATH(String path) {
sourceVideoPath = path;
}
/**
* 视频截图
*/
public boolean processImg(String sourceVideoPath) {
System.out.println("截图===得到原视频路径sourceVideoPath===="+sourceVideoPath);
//先确保保存截图的文件夹存在
File TempFile = new File(imageRealPath);//imageRealPath为截图存放路径
System.out.println("截图===截图存放路径imageRealPath===="+imageRealPath);
if (TempFile.exists()) {
if (TempFile.isDirectory()) {
System.out.println("该文件夹存在。");
}else {
System.out.println("同名的文件存在,不能创建文件夹。");
}
}else {
System.out.println("文件夹不存在,创建该文件夹。");
TempFile.mkdir();
}
File fi = new File(sourceVideoPath);
filename = fi.getName();//获取视频文件的名称。
filerealname = filename.substring(0, filename.lastIndexOf(".")); //获取视频名+不加后缀名 后面加.toLowerCase()转为小写
List<String> commend = new ArrayList<String>();
//第一帧: 00:00:01
//截图命令:time ffmpeg -ss 00:00:02 -i test1.flv -f image2 -y test1.jpg
commend.add(ffmpegpath); //指定ffmpeg工具的路径
commend.add("-ss");
commend.add("00:00:02"); //2是代表第2秒的时候截图
commend.add("-i");
commend.add(sourceVideoPath); //截图的视频路径
commend.add("-f");
commend.add("image2");
commend.add("-y");
commend.add(imageRealPath + filerealname + ".png"); //生成截图xxx.png
System.out.println("截图===给commend添加值后,检验imageRealPath===="+imageRealPath);
System.out.println("截图===给commend添加值后,检验filerealname===="+filerealname);
//打印截图命令
StringBuffer test = new StringBuffer();
System.out.println("截图===commend.size=="+commend.size());
for (int i = 0; i < commend.size(); i++) {
test.append(commend.get(i) + " ");
System.out.println("截图===commend");
}
System.out.println("截图命令为:"+test);
//转码后完成截图功能-还是得用线程来解决
try {
//调用线程处理命令
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
Process p = builder.start();
//获取进程的标准输入流
final InputStream is1 = p.getInputStream();
//获取进程的错误流
final InputStream is2 = p.getErrorStream();
//启动两个线程,一个线程负责读标准输出流,另一个负责读标准错误流
new Thread() {
public void run() {
BufferedReader br = new BufferedReader(
new InputStreamReader(is1));
try {
String lineB = null;
while ((lineB = br.readLine()) != null) {
if (lineB != null){
System.out.println("截图===lineB========="+lineB); //必须取走线程信息避免堵塞
}
}
} catch (IOException e) {
e.printStackTrace();
}
//关闭流
finally{
try {
is1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}.start();
new Thread() {
public void run() {
BufferedReader br2 = new BufferedReader(
new InputStreamReader(is2));
try {
String lineC = null;
while ((lineC = br2.readLine()) != null) {
if (lineC != null) {
System.out.println("截图===lineC========="+lineC); //必须取走线程信息避免堵塞
}
}
} catch (IOException e) {
e.printStackTrace();
}
//关闭流
finally{
try {
is2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}.start();
p.waitFor();
System.out.println("截图进程结束");
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
OneVideoinformationJsonUtils.java文件,用于json解析。
package cn.lijx.mytools;
import java.util.ArrayList;
import java.util.List;
import cn.lijx.domain.Video;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
public class OneVideoinformationJsonUtils {
public static List<Video> OneVideoinformationJsonToList(String jsonString){
List<Video> list = new ArrayList<>();
System.out.println("OneVideoinformationJsonUtils,得到的jsonString为>>>>>>"+jsonString);
try {
JSONObject obj = new JSONObject(jsonString.substring(jsonString.indexOf("{"), jsonString.lastIndexOf("}")+1));
System.out.println("封装完成");
//将jsonObject中的value值封装成属性值
Video info=new Video();
info.setVideo_author(obj.getString("video_author"));
info.setVideo_title(obj.getString("video_title"));
info.setVideo_category(obj.getString("video_category"));
info.setVideo_source(obj.getString("video_source"));
info.setVideo_classes(obj.getString("video_classes"));
info.setVideo_introduce(obj.getString("video_introduce"));
info.setVideo_money(obj.getString("video_money"));
System.out.println("将value值封装成属性值完成");
list.add(info);
System.out.println("OneVideoinformationJsonUtils>>>>>>"+list);
} catch (JSONException e) {
e.printStackTrace();
}
return list;
}
}
Contants.java文件,用于设置文件存储路径。
package cn.lijx.mytools;
public class Contants {
public static final String videoRealPath = "D:/apache-tomcat-9.0.31/webapps/SDD/websiteimages/temp/"; // 需要被截图的视频目录
public static final String targetfolder = "D:/apache-tomcat-9.0.31/webapps/SDD/websiteimages/finshvideo/"; // 转码后视频保存的目录
public static final String imageRealPath = "D:/apache-tomcat-9.0.31/webapps/SDD/websiteimages/finshimg/"; // 截图的存放目录
}
服务器端代码写好啦!