package com.thhy.filectrl.utils;
|
|
import com.thhy.general.common.BasicStatus;
|
import com.thhy.general.exception.BasicException;
|
import org.apache.commons.io.IOUtils;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
|
import java.io.BufferedReader;
|
import java.io.IOException;
|
import java.io.InputStreamReader;
|
import java.util.concurrent.TimeUnit;
|
|
public class ConvertUtil {
|
|
private static Logger logger = LoggerFactory.getLogger(ConvertUtil.class);
|
|
/**
|
* 格式转换方法
|
* <p>
|
* 統一把文件转换成pdf文件
|
*
|
* @param workspace 文件格式转换命令 例:/usr/bin/unoconv -f pdf /app/1.pptx
|
* @throws Exception 格式转换过程中的异常
|
*/
|
public static void executeCommand(String workspace,String fileName){
|
|
StringBuffer output = new StringBuffer();
|
Process process;
|
InputStreamReader inputStreamReader = null;
|
BufferedReader reader = null;
|
try {
|
String command = "/usr/bin/unoconv -f pdf "+workspace+"/"+fileName;
|
logger.info("执行命令"+command);
|
process = Runtime.getRuntime().exec(command);
|
|
new Thread() {
|
@Override
|
public void run() {
|
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
String line = null;
|
|
try {
|
while ((line = in.readLine()) != null) {
|
logger.info("执行的结果为: "+line);
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
} finally {
|
try {
|
in.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
}.start();
|
|
new Thread(){
|
@Override
|
public void run()
|
{
|
BufferedReader err = new BufferedReader(new InputStreamReader(process.getErrorStream()));
|
String line = null;
|
StringBuilder result=new StringBuilder();
|
try
|
{
|
while((line = err.readLine()) != null)
|
{
|
result.append(line);
|
}
|
logger.info("错误信息:"+result);
|
}
|
catch (IOException e)
|
{
|
e.printStackTrace();
|
}
|
finally
|
{
|
try
|
{
|
err.close();
|
}
|
catch (IOException e)
|
{
|
e.printStackTrace();
|
}
|
}
|
}
|
}.start();
|
|
int ii = process.waitFor();
|
logger.info("执行命令完成"+ii);
|
|
/*if(!result){
|
logger.info("执行命令失败");
|
throw new BasicException(BasicStatus.FILE_CONVERT_FAILD);
|
}*/
|
if(ii!=0){
|
logger.info("执行命令失败");
|
throw new BasicException(BasicStatus.FILE_CONVERT_FAILD);
|
}
|
inputStreamReader = new InputStreamReader(process.getInputStream(), "UTF-8");
|
reader = new BufferedReader(inputStreamReader);
|
String line = "";
|
while ((line = reader.readLine()) != null) {
|
output.append(line + "\n");
|
}
|
//p.destroy();//这个一般不需要
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
IOUtils.closeQuietly(reader);
|
IOUtils.closeQuietly(inputStreamReader);
|
}
|
}
|
}
|