做项目的时候碰到一个需求 :用户上传的文件需要在线进行预览(最少要支持word pdf txt excel )的预览。
openoffice的下载和使用自行百度吧~
先将文档转为swf文件。
需要jar包为:
转换需要这个东西 (在代码用有注释哪里需要这个地址)
ps:
用maven的朋友可能找不到 2.2.2的jar 只有2.2.1 但是2.2.1又不能兼容高版本的office文件。
我尝试过2种解决方案都可以实现:1.pom.xml中指向本地的jar,自制maven的连接。2.下载jar包放入lib 然后buildPath。(有其他好方法告诉我啊)。
以下是转换类,细节看注释
1 packagecom.common;2
3 importjava.io.BufferedReader;4 importjava.io.File;5 importjava.io.IOException;6 importjava.io.InputStream;7 importjava.io.InputStreamReader;8
9 importorg.apache.log4j.Logger;10
11 importcom.artofsolving.jodconverter.DocumentConverter;12 importcom.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;13 importcom.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;14 importcom.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;15
16 /**
17 * 文档转为swf工具类18 * 不支持中文路径 swf文件名不能为中文名19 *20 *@authorlwh 2018年9月25日 上午11:27:2921 */
22 public classDocConverter {23 private static Logger logger = Logger.getLogger(DocConverter.class);24
25 private static final int environment = 1;//环境1:windows,2:linux(涉及pdf2swf路径问题)
26 privateString fileString;27 private String outputPath = "";//输入路径,如果不设置就输出在默认位置(可不修改)
28 privateString fileName;29 privateFile pdfFile;30 privateFile swfFile;31 privateFile docFile;32 private String pdf2swfexePath = "";//pdf2swf.exe文件的位置33
34 publicDocConverter(String fileString, String outputPath, String pdf2swfexePath) {35 this.outputPath =outputPath;36 this.pdf2swfexePath =pdf2swfexePath;37 ini(fileString, outputPath);38 }39
40 publicDocConverter(String fileString) {41
42 ini(fileString);43 }44
45 /*
46 * 重新设置 file @param fileString47 */
48 public voidsetFile(String fileString) {49 ini(fileString);50 }51
52 /**
53 * 初始化 文件输出到默认位置 lwh 2018年9月27日 上午10:27:3654 *55 *@paramfileString56 */
57 private voidini(String fileString) {58 this.fileString =fileString;59 fileName = fileString.substring(0, fileString.lastIndexOf(".")).trim();60 docFile = newFile(fileString);61 String fn =String.valueOf(System.currentTimeMillis());62 pdfFile = new File(fn.trim() + ".pdf");63 swfFile = new File(fn.trim() + ".swf");64 }65
66 /**
67 * 文件输出到指定位置 lwh 2018年9月27日 上午10:27:1468 *69 *@paramfileString 待转换文件全路径70 *@paramoptPath 文件输出位置71 */
72 private voidini(String fileString, String optPath) {73
74 this.fileString =fileString;75 String fn =String.valueOf(System.currentTimeMillis());76 docFile = newFile(fileString);77 pdfFile = new File(optPath, fn + ".pdf");78 swfFile = new File(optPath, fn + ".swf");79 }80
81 /**
82 * 文档转为pdf lwh 2018年9月27日 上午10:28:2183 *84 *@throwsException85 */
86 private void doc2pdf() throwsException {87 if(docFile.exists()) {88 if (!pdfFile.exists()) {89 OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);90 try{91 connection.connect();92 DocumentConverter converter = newOpenOfficeDocumentConverter(connection);93 converter.convert(docFile, pdfFile);94 connection.disconnect();95 logger.info("****pdf转换成功,PDF输出:" + pdfFile.getPath() + "****");96
97 } catch(java.net.ConnectException e) {98 e.printStackTrace();99 throw new Exception("****swf转换异常,openoffice服务未启动!****");100 } catch(com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {101 e.printStackTrace();102 throw new Exception("****swf转换器异常,读取转换文件失败****");103 } catch(Exception e) {104 e.printStackTrace();105 throwe;106 }107 } else{108 throw new Exception("****已经转换为pdf,不需要再进行转化****");109 }110 } else{111 throw new Exception("****swf转换器异常,需要转换的文档不存在,无法转换****");112 }113 }114
115 /**
116 * pdf转为swf文件117 * lwh118 * 2018年9月27日 上午10:28:31119 *@throwsException120 */
121 @SuppressWarnings("unused")122 private void pdf2swf() throwsException {123 Runtime r =Runtime.getRuntime();124 if (!swfFile.exists()) {125 if(pdfFile.exists()) {126 if (1 == environment)//windows环境处理
127 {128 try{129 //这里根据SWFTools安装路径需要进行相应更改 修改2
130 Process p = r.exec(pdf2swfexePath + "/pdf2swf.exe " + pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9");131 logger.info(loadStream(p.getInputStream()));132 logger.info(loadStream(p.getErrorStream()));133 logger.info(loadStream(p.getInputStream()));134 logger.info("****swf转换成功,文件输出:" + swfFile.getPath() + "****");135 if(pdfFile.exists()) {136 pdfFile.delete();137 }138 } catch(Exception e) {139 e.printStackTrace();140 throwe;141 }142 } else if (environment == 2)//linux环境处理
143 {144 try{145 Process p = r.exec("pdf2swf " + pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9");146 logger.info(loadStream(p.getInputStream()));147 logger.info(loadStream(p.getErrorStream()));148 logger.info("****swf转换成功,文件输出:" + swfFile.getPath() + "****");149 if(pdfFile.exists()) {150 pdfFile.delete();151 }152 } catch(Exception e) {153 e.printStackTrace();154 throwe;155 }156 }157 } else{158 throw new Exception("****pdf不存在,无法转换****");159 }160 } else{161 throw new Exception("****swf已存在不需要转换****");162 }163 }164
165 static String loadStream(InputStream in) throwsIOException {166 int ptr = 0;167 //把InputStream字节流 替换为BufferedReader字符流
168 BufferedReader reader = new BufferedReader(new InputStreamReader(in, "GB2312"));169 StringBuilder buffer = newStringBuilder();170 while ((ptr = reader.read()) != -1) {171 buffer.append((char) ptr);172 }173 returnbuffer.toString();174 }175
176 /**
177 * 转换主方法178 * lwh179 * 2018年9月27日 上午10:28:47180 *@return
181 *@throwsException182 */
183
184 @SuppressWarnings("unused")185 public boolean conver() throwsException {186 boolean flag = false;187 if(swfFile.exists()) {188 logger.info("****swf转换器开始工作,该文件已经转换为swf****");189 flag = true;190 }191 if (environment == 1) {192 logger.info("****swf转换器开始工作,当前设置运行环境windows****");193 } else{194 logger.info("****swf转换器开始工作,当前设置运行环境linux****");195 }196 try{197 doc2pdf();198 pdf2swf();199 } catch (Exception e) { //TODO: Auto-generated catch block
200 e.printStackTrace();201
202 flag = false;203 throw newException(e.getMessage());204 }205 if(swfFile.exists()) {206 flag = true;207 } else{208 flag = false;209 }210 returnflag;211 }212
213 /**
214 * 返回转换 后的swf文件的路径215 * lwh216 * 2018年9月27日 上午10:29:05217 *@return
218 */
219 publicString getswfPath() {220 if(swfFile.exists()) {221 String tempString =swfFile.getPath();222 tempString = tempString.replaceAll("\\\\", "/");223 returntempString;224 } else{225 return "";226 }227
228 }229
230 /*
231 * 设置输出路径232 */public voidsetOutputPath(String outputPath) {233 this.outputPath =outputPath;234 if (!outputPath.equals("")) {235 String realName = fileName.substring(fileName.lastIndexOf("/"), fileName.lastIndexOf("."));236 if (outputPath.charAt(outputPath.length()) == '/') {237 swfFile = new File(outputPath + realName + ".swf");238 } else{239 swfFile = new File(outputPath + realName + ".swf");240 }241 }242 }243
244 public static voidmain(String[] args) {245 //修改1-不支持中文路径和中文文档246 //DocConverter d = new DocConverter("D://testfile/test2.pptx");247 //d.conver();
248 }249 }
Bean定义(属性是业务需求 ,自行修改)
packagecom.main.pojo;importjava.io.Serializable;importcom.auto.annotation.Column;importcom.auto.annotation.Table;/*** 系统附件表
*
*@authorlwh
**/@Table(name="TZW_ATTACHMENT",jsname="附件",includeSupperClass=false)public class Attachment implementsSerializable {/****/
private static final long serialVersionUID = 1L;
@Column(flag= "primary")private String id; //附件id
@Columnprivate String asocciateId;//附件所属id
@Column(type = "varchar(200)")private String fileName;//文件名
@Column(type = "varchar(200)")private String fileUrl;//文件路径
@Column(type = "int")private Integer attachmentType;//附件类型
@Column(type = "varchar(200)")private String memo;//备注
/*** 附件类型定义。例如:凭证附件 值为1
*@authorlwh
**/
public static final classAttachmentType {/*** 凭证附件*/
public final static Integer VOUCHER = 1;
}publicString getId() {returnid;
}public voidsetId(String id) {this.id =id;
}publicString getAsocciateId() {returnasocciateId;
}public voidsetAsocciateId(String asocciateId) {this.asocciateId =asocciateId;
}publicString getFileUrl() {returnfileUrl;
}public voidsetFileUrl(String fileUrl) {this.fileUrl =fileUrl;
}publicString getFileName() {returnfileName;
}public voidsetFileName(String fileName) {this.fileName =fileName;
}publicInteger getAttachmentType() {returnattachmentType;
}public voidsetAttachmentType(Integer attachmentType) {this.attachmentType =attachmentType;
}publicString getMemo() {returnmemo;
}public voidsetMemo(String memo) {this.memo =memo;
}
}
以下是controller的代码(代码中有一段if是用来控制图片预览的 isPic==false 这段就是文档预览的实现 )
packagecom.main.controller;importjava.io.File;importjava.util.ArrayList;importjava.util.List;importjavax.annotation.Resource;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.PathVariable;importorg.springframework.web.bind.annotation.RequestMapping;importcom.common.CommonUtil;importcom.common.DocConverter;importcom.main.pojo.Attachment;importcom.main.service.AttachmentService;/*** 附件预览controller 基于OppenOfiice实现
*
*@authorlwh 2018年9月21日 下午3:28:38*/@Controller
@RequestMapping("/attachmentPreview")public classAttachmentPreviewController {
@ResourceprivateAttachmentService attachmentService;/*** 附件预览(支持excel word pdf txt ppt pdf png gif bpm jpg) lwh 2018年10月11日
* 下午4:25:55
*
*@paramid
* 附件
*@paramrequest
*@paramresponse
*@return
*/@RequestMapping("/filePreview/{id}")publicString toFilePreviewPage(@PathVariable String id, HttpServletRequest request, HttpServletResponse response) {boolean res = false;
String contexPath= request.getSession().getServletContext().getRealPath("/");boolean isPic = false;//判断是否为图片预览 ·
try{
Attachment att=attachmentService.getById(id);if(isPicture(att.getFileName())) {
List attList = new ArrayList<>();
attList.add(att);//TODO
/**
* 为了避免预览时大量下载造成压力故图片预览的时候 不做图片下载IO操作, 通过配置虚拟目录读取,将以下配置加入
* tomcat的server.xml的中
* docBase="E:\apache-tomcat-7.0.90\financeAttatch\"
* docBase为tomcat下的附件存放目录的绝对路径 reloadable="true">
* 如果配置nginx或者其他负载均衡等。。。 需要将虚拟目录加入其配置文件,否则可能会被拦截,导致图片无法显示*/isPic= true;
List attListTemp =attachmentService.getPictureAttachment(att.getAsocciateId(), att.getId());/** 设置当前点击图片为第一张显示*/
if (attListTemp != null && attListTemp.size() > 0) {for(Attachment attachment : attListTemp) {
attList.add(attachment);
}
}
request.setAttribute("list", attList);
}else{//TODO//服务器上需要安装openoffice 并启动服务.
DocConverter d = new DocConverter(att.getFileUrl() + File.separator + att.getFileName(), contexPath + "/resources/file/ftp", contexPath+ "/WEB-INF/classes");
delete(contexPath+ "/resources/file/ftp");//调用conver方法开始转换,先执行doc2pdf()将office文件转换为pdf;再执行//pdf2swf()将pdf转换为swf;
res =d.conver();
String fn=d.getswfPath();if(CommonUtil.isNotNullStr(fn)) {
request.setAttribute("swfPath", fn.substring(fn.lastIndexOf("/"), fn.length()));
}
}
}catch(Exception e) {
request.setAttribute("errorMsg", e.getMessage());
e.printStackTrace();
}if (res == false && isPic == false) {//类型不支持预览。
return "attach/Cantpreview";
}if(isPic) {return "attach/PicPreview";//图片预览
}return "attach/FilePreview";//文档预览
}/*** 清除临时文件夹中的文件。*/
protected booleandelete(String path) {boolean flag = false;
File file= newFile(path);if (!file.exists()) {returnflag;
}if (!file.isDirectory()) {returnflag;
}
String[] tempList=file.list();
File temp= null;for (int i = 0; i < tempList.length; i++) {if(path.endsWith(File.separator)) {
temp= new File(path +tempList[i]);
}else{
temp= new File(path + File.separator +tempList[i]);
}if(temp.isFile()) {
temp.delete();
}if(temp.isDirectory()) {
delete(path+ "/" + tempList[i]);//先删除文件夹里面的文件
delete(path + "/" + tempList[i]);//再删除空文件夹
flag = true;
}
}returnflag;
}/*** 要预览其他格式的图片,将图片的后缀加入条件即可,支持预览html img标签src="支持的图片格式" lwh 2018年10月15日
* 上午11:38:25
*
*@paramfileName
*@return
*/
public booleanisPicture(String fileName) {boolean flag = false;if (fileName.endsWith(".jpg") || fileName.endsWith(".bpm") || fileName.endsWith(".png") || fileName.endsWith(".gif")) {
flag= true;
}returnflag;
}
}
以上服务器端代码基本就是这样了。下面是页面代码
flexpaper插件自行下载一下 导入对应的js即可。
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
String swfName = String.valueOf(request.getAttribute("swfPath"));
%>
凭证录入href="/resources/style/record-background.css" type="text/css" />
双击文本 可放大查看!
$("#documentViewer").css("height", document.documentElement.clientHeight-40);
$('#documentViewer').FlexPaperViewer(
{ config : {
SWFFile :'/resources/file/ftp',
Scale : 0.6,
ZoomTransition : 'easeOut',
ZoomTime : 0.5,
ZoomInterval : 0.2,
FitPageOnLoad : true,
FitWidthOnLoad : false,
FullScreenAsMaxWindow : false,
ProgressiveLoading : false,
MinZoomSize : 0.2,
MaxZoomSize : 5,
SearchMatchAll : false,
InitViewMode : 'Portrait',
RenderingOrder : 'flash',
StartAtPage : '',
ViewModeToolsVisible : true,
ZoomToolsVisible : true,
NavToolsVisible : true,
CursorToolsVisible : true,
SearchToolsVisible : true,
WMode : 'window',
localeChain: 'zh_CN'
}}
);