package com.doone.knowledge.info.action;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
import org.apache.struts2.ServletActionContext;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import com.doone.comm.paginaction.Pagination;
import com.doone.comm.struts.BaseAction;
import com.doone.knowledge.info.dao.KnowledgeInfoDao;
/**
* 课件资源管理
* @author hsp
*
*/
public class KnowledgeInfoAction2 extends BaseAction{
private Pagination page = new Pagination(); // 分页对象
private KnowledgeInfoDao knowledgeInfoFacade;
private HashMap<String,Object> knowledgeInfoMap = new HashMap<String,Object>();
/**
* 获取知识信息
* @return
*/
@SuppressWarnings("unchecked")
public String seeKnowledgeInfo() throws Exception{
String id=this.getRequest().getParameter("kinfoid");
//信息评论分页:
//设置每页显示多少条
page.setPageRecord(5);
//求出总记录数并判断是否为第一次查询
if(getQueryFlag("page") == -1) {
page.setTotalRecord(knowledgeInfoFacade.getKnowledgeRecommentCount(id));
} else {
page.setTotalRecord(getQueryFlag("page"));
}
//设置查询条件
this.setPageParameters(page, "page");
//结果List
HashMap infoMap = knowledgeInfoFacade.getKnowledgeInfoInfo(id, page.getPageStart(), page.getPageRecord());
knowledgeInfoMap=(HashMap)infoMap.get("kinfo");
if(infoMap.get("krecomment")!=null&&!"".equals(infoMap.get("krecomment"))){
List list=(List)infoMap.get("krecomment");
this.getRequest().setAttribute("recommentList", list);
}
//文件类别
String fileType =this.knowledgeInfoMap.get("KNOWLEDGE_FILE_TYPE").toString().substring(1, knowledgeInfoMap.get("KNOWLEDGE_FILE_TYPE").toString().length());
//是否可预览标志 0普通文档,MP3,SWF,可以预览 , 1 不支持的文件类型,不能预览 2,MP4播放
String previewFlag="";
String returnValue="";
//可以预览的文件
if(fileType.equals("XLS") || fileType.equals("PPT") || fileType.equals("DOC") || fileType.equals("PDF") || fileType.equals("TXT") || fileType.equals("MP3") || fileType.equals("SWF")){
previewFlag = "0";
returnValue="nomp4";
}else if(fileType.indexOf(".MP4")>0){
returnValue="mp4";
previewFlag = "2";
}else{
returnValue="nomp4";
previewFlag = "1";
}
System.out.println("fileType==========="+fileType+"=========="+ previewFlag+ previewFlag.equals("0"));
/**************************************以下为文件预览*****************************************************/
if(previewFlag.equals("0")){
//服务器信息
Properties props=System.getProperties();
//不同系统分隔符
String separator=props.getProperty("file.separator");
//系统版本 Linux,Window XP,Window 7
String os_name=props.getProperty("os.name");
String os_flag="";
if(os_name.indexOf("Linux")==0){
os_flag="linux";
}else if(os_name.indexOf("Windows")==0){
os_flag="window";
}
//服务器存储的文件名称 201212171709212889.doc
String sysFileName=this.knowledgeInfoMap.get("KNOWLEDGE_PATH").toString();
//文件原名 年会活动初稿请车友定稿.doc
String oriFileName =this.knowledgeInfoMap.get("KNOWLEDGE_FNAME").toString();
//文件完整路径
//window下是一个反斜杠 \
//linux 下 是一个正斜杠/home/apache-tomcat-6.0.33/webapps/jd/WebRoot/upload/knowledge/201212181318342606.ppt
String fullPath =ServletActionContext.getServletContext().getRealPath(separator+"upload"+separator+"knowledge"+separator+sysFileName);
//文件夹名称绝对路径 F:\\workspace\\doone-jdedu\\WebRoot\\upload\\knowledge
String folderName=fullPath.substring(0,fullPath.lastIndexOf(separator));
//与sysFileName保持一致,如果已经存在,不再每次生成
String sysFileName_temp = sysFileName.substring(0,sysFileName.lastIndexOf("."));
//工程的绝对路径
String basePath = this.getRequest().getScheme()+"://"+this.getRequest().getServerName()+":"+this.getRequest().getServerPort()+this.getRequest().getContextPath();
File sourceFile; //转换源文件
File pdfFile; //PDF媒介文件
File swfFile; //SWF目标文件
File createPath; //创建文件存放目录
Runtime rt; //转换命令执行类
String finalFilePath = ""; //最终显示在前台的转换之后的SWF文件名称
String swfToolsPath =""; //swfTools 安装路径,
if(os_flag.equals("linux")){
swfToolsPath="";
}else{
swfToolsPath ="C:\\Program Files\\SWFTools\\";
}
//swf
if(fileType.equals("SWF")){
//文件原路径
finalFilePath = basePath+separator+"upload"+separator+"knowledge"+separator+sysFileName;
System.out.println("It's SWF=================="+finalFilePath);
}
//pdf
if(fileType.equals("PDF")){
//PDF格式文件处理方式
rt = Runtime.getRuntime();
sourceFile = new File(fullPath);
//创建SWF文件存放目录
createPath = new File(folderName+separator+"swfFiles"+separator);
if(!createPath.isDirectory()){
createPath.mkdir();
}
swfFile = new File(folderName+separator+"swfFiles"+separator+sysFileName_temp+".swf");
//为了避免版本问题,不用7,8版本,用9
Process p= null;
if(os_flag.equals("window")){
p = rt.exec(swfToolsPath+"pdf2swf.exe " + sourceFile.getPath() + " -o " + swfFile.getPath() + " -T 9");
}else{
p = rt.exec("pdf2swf -s languagedir=/usr/local/xpdf-chinese-simplified -T 9 -s poly2bitmap -s zoom=150 -s flashversion=9 "+ sourceFile.getPath() +" -o "+swfFile.getPath());
}
//各系统清理缓存
if(os_flag.equals("window")){
clearCache(p.getInputStream(), p.getErrorStream());
}else if(os_flag.equals("linux")){
InputStreamReader ir=new InputStreamReader(p.getInputStream());
LineNumberReader input = new LineNumberReader (ir);
String line;
while ((line = input.readLine ()) != null){
System.out.println("linux clear===================="+line);
}
}
finalFilePath = basePath+separator+"upload"+separator+"knowledge"+separator+"swfFiles"+separator+sysFileName_temp+".swf";
System.out.println("It's PDF=========sourceFile"+sourceFile.getPath()+"========="+swfFile+"========="+finalFilePath);
}
//非pdf,非swf
else{
System.out.println("It's "+fileType+"===================="+sysFileName_temp);
//非PDF格式文件处理方式
if(isLegal(fileType.toUpperCase())){
sourceFile = new File(fullPath);
pdfFile = new File(folderName+separator+"swfFiles"+separator+sysFileName_temp+".pdf");
swfFile = new File(folderName+separator+"swfFiles"+separator+sysFileName_temp+".swf");
if(!pdfFile.exists()){
//获取连接对象
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
//取得连接
connection.connect();
//创建文件格式转换对象
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
//实现文件格式转换
converter.convert(sourceFile, pdfFile);
//生成已转换的PDF文件
pdfFile.createNewFile();
//释放连接
connection.disconnect();
System.out.println("create a new pdf=========="+pdfFile.getParent());
if(!swfFile.exists()){
rt = Runtime.getRuntime();
//执行PDF文件转换成SWF文件命令
//linux命令: pdf2swf -s languagedir=/usr/local/xpdf-chinese-simplified -T 9 -s poly2bitmap -s zoom=150
- 1
- 2
前往页