importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.util.HashSet;importjava.util.Set;importorg.apache.log4j.Logger;importorg.springframework.util.Assert;importch.ethz.ssh2.Connection;importch.ethz.ssh2.Session;importch.ethz.ssh2.StreamGobbler;public classConvertUtil {private static Logger log = Logger.getLogger(SendMessageUtil.class);/*** Method name: convertOfficeToSwf
* Description: 文件转换方法,可以把.doc .docx .ppt .pptx .xls .xlsx 文件转换成.swf文件
* Remark:
*@paramfilepath 传入文件的路径及文件名(最前面不要带"/") 示例: aa/bb.docx
*@returnboolean 发送结果 true:转换成功,false:转换失败
*/@SuppressWarnings("resource")public static booleanconvertOfficeToSwf(String filepath) {//定义服务器的地址、用户名、密码
String hostname = PropertyUtil.getConfigureProperties("SERVER_HOST");
String username= PropertyUtil.getConfigureProperties("USER_NAME");
String password= PropertyUtil.getConfigureProperties("PASS_WORD") + PropertyUtil.getDatabaseProperties("keyword");//从property文件中获取路径前辍
String filepath_prefix =PropertyUtil
.getConfigureProperties("FILE_PATH");
filepath= filepath_prefix +filepath;
String fileName= filepath.substring(0, filepath.lastIndexOf("."));//拼出pdf和swf文件的路径
String pdfFile = fileName + ".pdf";
String swfFile= fileName + ".swf";
log.info("pdf file path: " +pdfFile);
log.info("swf file path: " +swfFile);
String commadStr= "java -jar /home/hxj/jodconverter-2.2.2/lib/jodconverter-cli-2.2.2.jar "
+filepath+ " "
+pdfFile+ ";export LD_LIBRARY_PATH=/usr/local/lib;pdf2swf "
//+ ";pdf2swf "
+pdfFile+ " -o "
+swfFile;
log.info("command string: " +commadStr);//指明连接主机的IP地址
Connection conn = newConnection(hostname);
Session ssh= null;try{//连接到主机
conn.connect();//使用用户名和密码校验
boolean isconn =conn.authenticateWithPassword(username, password);if (!isconn) {
log.info("ssh connect fail,username or password error");
}else{
log.info("ssh connect success");
ssh=conn.openSession();//使用多个命令用分号隔开
ssh.execCommand(commadStr.toString());//只允许使用一行命令,即ssh对象只能使用一次execCommand这个方法,多次使用则会出现异常//将屏幕上的文字全部打印出来
InputStream stdout= newStreamGobbler(ssh.getStdout());
InputStream stderr= newStreamGobbler(ssh.getStderr());
BufferedReader stdoutReader= newBufferedReader(newInputStreamReader(stdout));
BufferedReader stderrReader= newBufferedReader(newInputStreamReader(stderr));
log.info("Here is the output from stdout:");while (true) {
String line=stdoutReader.readLine();if (line == null)break;
log.info(line);
}
log.info("Here is the output from stderr:");while (true) {
String line=stderrReader.readLine();if (line == null)break;
log.info(line);
}
log.info("ssh finish");
}//连接的Session和Connection对象都需要关闭
ssh.close();
conn.close();return true;
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
log.error("file convert fail");return false;
}
}//视频转码后的文件后缀
private static final String FLV_FILE_EXT = ".flv";//视频转码开关: on表示开启;off表示关闭
private static final String VIDEO_CONVERT_SWITCH_ON = "on";//支持的视频类型文件后缀名,暂时只有一种,可以按需添加//注意需要对应修改相关的上传控件的参数:添加对应的后缀名
private static final SetSUPPORTED_VIDEO_TYPE_SET;static{
SUPPORTED_VIDEO_TYPE_SET= new HashSet();
SUPPORTED_VIDEO_TYPE_SET.add(".mp4");
}/***@authorJFTT)wangyifeng
* Method name: needConvertToFlv
* Description: 判断是否需要进行视频转码
* Remark:
*@paramfileExt 文件后缀名
*@returnboolean 返回传入的文件后缀类型是否需要进行视频转码:true需要;false不需要
*/
public static booleanneedConvertToFlv(String fileExt) {
String VIDEO_CONVERT_SWITCH=PropertyUtil
.getConfigureProperties("VIDEO_CONVERT_SWITCH");
log.info(String.format("VIDEO_CONVERT_SWITCH value is %s.",
VIDEO_CONVERT_SWITCH));//视频转码开关不等于VIDEO_CONVERT_SWITCH_ON时,固定返回false,即认为不需要转码
if (!VIDEO_CONVERT_SWITCH_ON.equals(VIDEO_CONVERT_SWITCH)) {return false;
}
Assert.hasText(fileExt);returnSUPPORTED_VIDEO_TYPE_SET.contains(fileExt.toLowerCase());
}/***@authorJFTT)wangyifeng
* class name:ConvertToFlvFailedException
* class description: 视频转码异常类(运行时异常)
* Remark:
*@version1.00 2015/09/24*/
public static class ConvertToFlvFailedException extendsRuntimeException {private static final long serialVersionUID = 1L;publicConvertToFlvFailedException(String message) {super(message);
}publicConvertToFlvFailedException(String message, Throwable e) {super(message, e);
}
}/*** Method name: convertToFlv
* Description: 视频文件转换方法,把常用的视频文件如:mp4,avi,3gp等转换成flv文件用于网页播放
* Remark:
* 1.调用该方法前,请先用needToBeConvertedToFlv方法来确认待转换文件是否支持视频转码
* 2.视频转码失败时会抛出运行时异常:ConvertToFlvFailedException
*@paramfilepath 传入文件的路径及文件名(最前面不要带"/") 示例: aa/bb.docx
*@returnString 发送结果 转换成功:转换后的文件路径及文件名;转换失败:抛出ConvertToFlvFailedException异常
*/@SuppressWarnings("resource")public staticString convertToFlv(String filepath) {//定义服务器的地址、用户名、密码
String hostname = PropertyUtil.getConfigureProperties("SERVER_HOST");
String username= PropertyUtil.getConfigureProperties("USER_NAME");
String password= PropertyUtil.getConfigureProperties("PASS_WORD")+ PropertyUtil.getDatabaseProperties("keyword");//从property文件中获取路径前辍
String filepath_prefix =PropertyUtil
.getConfigureProperties("FILE_PATH");
log.info("input video file: " + filepath_prefix +filepath);//拿到文件扩展名
String fileExt = filepath.substring(filepath.lastIndexOf("."),
filepath.length());//如果获取到的扩展名经判断无需转码,则扔异常
if (!needConvertToFlv(fileExt)) {throw newConvertToFlvFailedException(String.format("传入的视频文件不需要转码: %s.", filepath_prefix +filepath));
}
String fileName= filepath.substring(0, filepath.lastIndexOf("."));//拼出flv文件的路径
String flvFile = fileName +FLV_FILE_EXT;
log.info("flv file path: " + filepath_prefix +flvFile);//拼接命令字符串
String commadStr = "ffmpeg -i "
+filepath_prefix+filepath+ " "
+ "-ab 128 -acodec libmp3lame -ac 2 -ar 22050 -qscale 10 -s 640*480 -r 15 "
+ filepath_prefix +flvFile;
log.info("command string: " +commadStr);//指明连接主机的IP地址
Connection conn = newConnection(hostname);
Session ssh= null;try{//连接到主机
conn.connect();//使用用户名和密码校验
boolean isconn =conn.authenticateWithPassword(username, password);if (!isconn) {
log.info("ssh connect fail,username or password error");throw newConvertToFlvFailedException("ssh connect fail,username or password error");
}
log.info("ssh connect success");
ssh=conn.openSession();//使用多个命令用分号隔开
ssh.execCommand(commadStr.toString());//只允许使用一行命令,即ssh对象只能使用一次execCommand这个方法,多次使用则会出现异常//将屏幕上的文字全部打印出来
InputStream stdout= newStreamGobbler(ssh.getStdout());
InputStream stderr= newStreamGobbler(ssh.getStderr());
BufferedReader stdoutReader= newBufferedReader(newInputStreamReader(stdout));
BufferedReader stderrReader= newBufferedReader(newInputStreamReader(stderr));
log.info("Here is the output from stdout:");while (true) {
String line=stdoutReader.readLine();if (line == null)break;
log.info(line);
}
log.info("Here is the output from stderr:");while (true) {
String line=stderrReader.readLine();if (line == null)break;
log.info(line);
}
log.info(String.format("ssh finished with exit status: %d",
ssh.getExitStatus()));if (ssh.getExitStatus() != 0) {throw newConvertToFlvFailedException("file convert fail: command exit code is not 0!");
}returnflvFile;
}catch(IOException e) {
log.error("file convert fail", e);throw new ConvertToFlvFailedException("file convert fail", e);
}finally{//连接的Session和Connection对象都需要关闭
if (ssh != null) {
ssh.close();
}if (conn != null) {
conn.close();
}
}
}public static voidmain(String[] args) {//TODO Auto-generated method stub
convertOfficeToSwf("a.docx");//convertOfficeToSwf("b2.xlsx");//convertOfficeToSwf("c1.ppt");//convertOfficeToSwf("c2.pptx");
}
}