需求:用戶上傳附件後,點擊查看,可以在頁面直接查看到附件內容,樣式排版需要和附件文檔裏一致。另外可以查看附件信息,下載附件。
附件格式 爲 excle word 文檔,pdf 掃描件
分析:一個附件管理的功能 + 在線查看功能。
附件管理的功能好實現,略過。
在線查看,是通過一個播放器查看flash文件,網上例子很多。
flash播放器 搜索 找到了 FlexPaper (項目中導入flexpaper文件,在顯示頁面指定路徑即可顯示)
- <script type="text/javascript">
- var fp = new FlexPaperViewer(
- 'hlbussiness/planbaseViewer/FlexPaperViewer',
- 'viewerPlaceHolder', { config : {
- SwfFile : escape("${swfPath}"),<span style="white-space:pre"> </span>//swf文件路徑
- Scale : 0.6,
- ZoomTransition : 'easeOut',
- ZoomTime : 0.5,
- ZoomInterval : 0.2,
- FitPageOnLoad : true,
- FitWidthOnLoad : true, //適合初始頁寬度大小的裝載頁
- FullScreenAsMaxWindow : true,
- ProgressiveLoading : false,
- MinZoomSize : 0.2,
- MaxZoomSize : 5,
- SearchMatchAll : false,
- InitViewMode : 'Portrait',
- PrintPaperAsBitmap : false,
- ViewModeToolsVisible : true,
- ZoomToolsVisible : true,
- NavToolsVisible : true,
- CursorToolsVisible : true,
- SearchToolsVisible : true,
- localeChain: 'zh_CN'
- }});
- </script>
- package com.function.util;
- import java.io.BufferedInputStream;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.ConnectException;
- import java.util.ResourceBundle;
- 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;
- /**
- * 將文件轉成swf格式
- *
- * @author Administrator
- *
- */
- public class ConvertSwf {
- /**
- * 入口方法-通過此方法轉換文件至swf格式
- * @param filePath 上傳文件所在文件夾的絕對路徑
- * @param dirPath 文件夾名稱
- * @param fileName 文件名稱
- * @return 生成swf文件名
- */
- public String beginConvert(String filePath, String dirName, String fileName) {
- final String DOC = ".doc";
- final String DOCX = ".docx";
- final String XLS = ".xls";
- final String XLSX = ".xlsx";
- final String PDF = ".pdf";
- final String SWF = ".swf";
- final String TOOL = "pdf2swf.exe";
- String outFile = "";
- String fileNameOnly = "";
- String fileExt = "";
- if (null != fileName && fileName.indexOf(".") > 0) {
- int index = fileName.indexOf(".");
- fileNameOnly = fileName.substring(0, index);
- fileExt = fileName.substring(index).toLowerCase();
- }
- String inputFile = filePath + File.separator + fileName;
- String outputFile = "";
- //如果是office文檔,先轉爲pdf文件
- if (fileExt.equals(DOC) || fileExt.equals(DOCX) || fileExt.equals(XLS)
- || fileExt.equals(XLSX)) {
- outputFile = filePath + File.separator + fileNameOnly + PDF;
- office2PDF(inputFile, outputFile);
- inputFile = outputFile;
- fileExt = PDF;
- }
- if (fileExt.equals(PDF)) {
- String toolFile = filePath + File.separator + TOOL;
- outputFile = filePath + File.separator + fileNameOnly + SWF;
- convertPdf2Swf(inputFile, outputFile, toolFile);
- outFile = outputFile;
- }
- return outFile;
- }
- /**
- * 將pdf文件轉換成swf文件
- * @param sourceFile pdf文件絕對路徑
- * @param outFile swf文件絕對路徑
- * @param toolFile 轉換工具絕對路徑
- */
- private void convertPdf2Swf(String sourceFile, String outFile,
- String toolFile) {
- String command = toolFile + " \"" + sourceFile + "\" -o \"" + outFile
- + "\" -s flashversion=9 ";
- try {
- Process process = Runtime.getRuntime().exec(command);
- System.out.println(loadStream(process.getInputStream()));
- System.err.println(loadStream(process.getErrorStream()));
- System.out.println(loadStream(process.getInputStream()));
- System.out.println("###--Msg: swf 轉換成功");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * office文檔轉pdf文件
- * @param sourceFile office文檔絕對路徑
- * @param destFile pdf文件絕對路徑
- * @return
- */
- private int office2PDF(String sourceFile, String destFile) {
- ResourceBundle rb = ResourceBundle.getBundle("OpenOfficeService");
- String OpenOffice_HOME = rb.getString("OO_HOME");
- String host_Str = rb.getString("oo_host");
- String port_Str = rb.getString("oo_port");
- try {
- File inputFile = new File(sourceFile);
- if (!inputFile.exists()) {
- return -1; // 找不到源文件
- }
- // 如果目標路徑不存在, 則新建該路徑
- File outputFile = new File(destFile);
- if (!outputFile.getParentFile().exists()) {
- outputFile.getParentFile().mkdirs();
- }
- // 啓動OpenOffice的服務
- String command = OpenOffice_HOME
- + "/program/soffice.exe -headless -accept=\"socket,host="
- + host_Str + ",port=" + port_Str + ";urp;\"";
- System.out.println("###\n" + command);
- Process pro = Runtime.getRuntime().exec(command);
- // 連接openoffice服務
- OpenOfficeConnection connection = new SocketOpenOfficeConnection(
- host_Str, Integer.parseInt(port_Str));
- connection.connect();
- // 轉換
- DocumentConverter converter = new OpenOfficeDocumentConverter(
- connection);
- converter.convert(inputFile, outputFile);
- // 關閉連接和服務
- connection.disconnect();
- pro.destroy();
- return 0;
- } catch (FileNotFoundException e) {
- System.out.println("文件未找到!");
- e.printStackTrace();
- return -1;
- } catch (ConnectException e) {
- System.out.println("OpenOffice服務監聽異常!");
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return 1;
- }
- static String loadStream(InputStream in) throws IOException{
- int ptr = 0;
- in = new BufferedInputStream(in);
- StringBuffer buffer = new StringBuffer();
- while ((ptr=in.read())!= -1){
- buffer.append((char)ptr);
- }
- return buffer.toString();
- }
- }
1、安裝openoffice,新建OpenOfficeService.properties文件,放到src目錄下
- OO_HOME = D:/Program Files/OpenOffice.org 3
- oo_host = 127.0.0.1
- oo_port =8100
2、下載 jodconverter-2.2.2 ,將lib目錄所有jar 複製到項目 lib目錄
3、 下載swftools-0.9.2.exe 安裝後,複製安裝目錄中到 pdf2swf.exe 到項目附件目錄中。
在action中
- //轉換上傳文件格式爲swf
- String outPath = new ConvertSwf().beginConvert(dirPath, dirName, fileName);
- System.out.println("生成swf文件:" + outPath);
即可生成文件名與原文件名一直的swf文件。