この記事では、主に添付ファイルプレビューのJava実装を紹介します。これには、OpenOffice、Swftools、Flexpaperが必要です。特定の手順は次のとおりです。
1。概要
主な原則
1.サードパーティツールOpenOfficeを介してWord、Excel、PPT、TXTおよびその他のファイルをPDFファイルに変換します
2。PDFファイルをswftoolsを介してSWF形式のファイルに変換します
3.フレックスペーパードキュメントコンポーネントを介してページに表示
2。インストールパッケージをダウンロードします
1.Openofficeは、Apacheの下でオープンで無料のワードプロセッシングソフトウェアです
住所:Apache Oppenoffice公式ウェブサイトダウンロードバージョン-3.4.1 http://www.openoffice.org/zh-cn/download/
2。Swftoolsは、FlashのSWFファイルを処理するために使用されるツールキットのセットです。 PDFファイルをSWFファイルに変換するために使用します!
住所をダウンロード:swftools公式ウェブサイトSWFTOOLS-2013-04-09-1007.exe http://www.swftools.org/download.htmlをダウンロードしてください
3.FlexPaperは、ブラウザにさまざまなドキュメントを表示するオープンソースの軽量コンポーネントです。
ダウンロードアドレス:Flexpaper公式ウェブサイトダウンロードバージョン1.5.1 https://flowpaper.com/download/
4.JodConverter Java OpenDocumentファイルコンバーター、JARパッケージのみを使用します
アドレスをダウンロード:jodcconverterダウンロードhttps://sourceforge.net/projects/jodconverter/files/
3.ファイルをインストールします
1.ダウンロードされたファイル(JodConverterを除く)をインストールすると、必要に応じてドライブ文字を設定できます! OpenOfficeのインストールが完了した後、使用するときにサービスを開く必要があることに注意してください。次に、コマンドで開く必要があります。
DOSウィンドウを開き、OpenOfficeインストールドライブレターを入力し、次のコードを入力してサービスを開始します。
ソフトウェア-headless -accept = "socket、host = 127.0.0.1、port = 8100; urp;" -nofirststartwizard
最後のコマンドの前で「 - 」に注意してください、それを間違って書かないでください!サービスを取得できない場合、プロジェクトは継続できません。
公式ウェブサイトの起動サービスのスクリーンショットは次のとおりです。
ローカルスクリーンショット:
3。開発プロセス
1.新しいプロジェクトを作成し、FlexpaperファイルにJSフォルダーをコピーします(Flexpaper_flash_debug.js、Flexpaper_flash.js、jquery.jsを含む、これら3つのJSファイルは、主にプレビューSWFファイルのプラグインです)。 FlexPaperViewer.swfをWebサイトルートディレクトリにコピーします(ファイルは、主にWebページでSWFファイルを再生するプレーヤーとして使用されます)。ディレクトリ構造は次のとおりです。
注:アップロードフォルダーを作成する必要があります
2。fileupload.jspを作成します
<%@ page Language = "Java" contentType = "text/html; charset = utf-8" pageencoding = "utf-8"%> <! <html> <head> <メタhttp-equiv = "content-type" content = "text/html; charset = utf-8"> <title>ドキュメントオンラインプレビューシステム</title> <style> body {margin-top:100px; #CE4614; font-size:0.9em; text-align:center;}#msg-box .logo {border-bottom:5px solid#ece5d9; margin-bottom:20px; padding-bottom:10px;} {マージントップ:20px;} </style> </head> <body> <div id = "msg-box"> <form name = "form1" method = "post" enctype = "multipart/form-data" action = "docuploadconvertaction.jsp"> <div>処理されるファイルをアップロードしてください。プロセスには数分かかる場合があります。ちょっと待ってください。 </div> <p> <input name = "file1" type = "file"> </p> <p> <入力タイプ= "indoct" name = "submit" bulation "> </p> </form> </div> </body> </html> </html> < 3.変換ページDocuploadConvertaction.jspを作成します
<%@page Language = "Java" contentType = "text/html; charset = utf-8" pageencoding = "utf-8"%> <%@page import = "java.io。 Import = "com.oreilly.servlet.multipart.defaultfilerenamepolicy"%> <%@page import = "com.cectsims.util.docconverter"%> <%@ページインポート= " COSコンポーネントは、Commons-Fileuploadアップロードに置き換えることができます。ファイルがアップロードされたら、アップロードフォルダーに保存します//アップロードパス情報System.out.println(savedirectory)を印刷します。 //各ファイル最大50m int maxpostsize = 50 * 1024 * 1024; // cosのデフォルトの命名戦略を使用し、重複名の後に1、2、3を追加します... dfpを追加して重複した名前を追加しない場合、defaultfilerenamepolicy dfp = new defaultfilerenamepolicy(); //応答のエンコードは「UTF-8」であり、デフォルトのファイル名競合解決戦略を使用してアップロードを実装します。 DFPの複製名が追加されていない場合、MultiPartRequestが上書きされます。 MultiPartRequest Multi = new MultiPartRequest(request、savedirectory、maxpostsize、 "utf-8"、dfp); // MultiPartRequest Multi = new MultiPartRequest(request、savedirectory、maxpostsize、 "utf-8"); //フィードバック情報列挙ファイル= multi.getFileNames(); while(files.hasmoreElements()){system.err.println( "ccc"); string name =(string)files.nextelement();ファイルf = multi.getFile(name); if(f!= null){string filename = multi.getFilesystemName(name); //アップロードされたファイル文字列extname = filename.substring(filename.lastindexof( "。")+1)の拡張子を取得します。 //フルパス文字列lastFileName = savedirectory + "//" + filename; //変換する必要があるファイル名を取得し、パス名の「/」を '/' string converfileName = savedirectory.replaceall( "////"、 "/")+"/"+filename; System.out.println(converfileName); //コンバージョンクラスのdocconverterを呼び出し、ファイルを渡してこのクラスのコンストラクターに変換されますdocconverter d = new docconverter(converfilename); //変換メソッドを呼び出して変換を開始するには、最初にdoc2pdf()を実行してオフィスファイルをPDFに変換します。次に、PDF2SWF()を実行してPDFをSWFに変換します。 D.Conver(); // cell getSwPath()メソッドと変換されたSWFファイルパスsystem.out.println(d.getswfpath()); // SWF相対パスを生成してFlexpaper Player String swfpath = "upload"+d.getswfpath()。substring(d.getswfpath()。lastindexof( "/")); System.out.println(swfpath); //相対パスをsession.setattribute( "swfpath"、swfpath)に入れます。 out.println( "アップロードされたファイル:"+lastFileName); out.println( "ファイルタイプ"+extname); out.println( "<hr>"); }}%> <!doctype html public " - // w3c // dtd html 4.01 transitional // en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv = "content-type"/html; charset = utf-8 "> <title> insert estert estere </title> <style> body {margin-top:100px; background:#fff; font-family:verdana、tahoma;} a {color:#ce4614;}#msg-box {color:#ce4614; font-size:0.9em; text-align:center;}#msg-box .logo {border-bottom:5px solid#ece5d9; margin-bottom:20px; padding-bottom:10px;} {マージントップ:20px;} </style> </head> <body> <div> <form name = "Viewform" id "form_swf" action = "documentview.jsp" method = "post"> <入力タイプ= 'preview' class = 'button suct'/> </form> </div> </html> </html> </html> </html> </html>ビューページdocumentview.jspを作成します
<%@ page Language = "Java" contentType = "text/html; charset = utf-8" pageencoding = "utf-8"%> <%string session.getattribute( "swfpath")。toString(); %> <!doctype html public " - // w3c // dtd html 4.01 transitional // en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv = "content-type" contep-type "/"/"/utpe"/ <script type = "text/javascript" src = "js/jquery.js"> </script> <script type = "text/javascript" src = "js/flexpaper_flash.js"> </script> <"text/javascript" src = "js/flex_flash_debug.js"> "> <> <> <> <> <Stoce" media = "screen"> html、body {height:100%; } body {margin:0;パディング:0;オーバーフロー:自動; } #flashcontent {display:none; } </style> <title>ドキュメントオンラインプレビューシステム</title> </head> <body> <div style = "position:aspolute; left:50px; top:10px;"> <a id = "viewerplaceholder"> </a> <スクリプトタイプ= "テキスト/javascript"> var fp = new flexpaperviewer( 'flexpaper fiewhower:' vieweerplacehower ' Escape( '<%= Swffilepath%>')、Scale:0.6、ZoomTransition: 'EaseOut'、Zoomtime:0.5、Zoominterval:0.2、FitPageOnload:True、FitWidThonload:Fals、FullScreenasmaxWindow:False、Progressiveloading:false、false、false:false:false、false:false、false、false:false、fals 「シングルページ」、viewmodetoolsvisible:true、zoomtoolsvisible:true、navtoolsvisible:true、cursortoolsvisible:true、searchtoolsvisible:true、localechain: 'en_us'}}); </script> </div> </body> </html> 5.変換クラスdocconverter.javaを作成します
パッケージcom.cectsims.util; java.io.bufferedinputStreamをインポートします。 java.io.fileをインポートします。 java.io.ioexceptionをインポートします。 java.io.inputStreamをインポートします。 com.artofsolving.jodconverter.documentconverterをインポートします。 com.artofsolving.jodconverter.openoffice.connection.openofficeconnectionをインポートします。 com.artofsolving.jodconverter.openoffice.connection.socketopenofficeConnectionをインポートします。 com.artofsolving.jodconverter.openoffice.converter.openofficedocumentconverterをインポートします。 / *** docxフォーマット変換*/パブリッククラスdoccConverter {private static final int環境= 1; //環境1:Windows 2:Linux private string fileString; //(PDF2SWFパスの問題のみを含む)プライベートストリング出力= ""; //入力パス、設定されていない場合、出力はデフォルトの場所にあります。プライベートファイルpdffile;プライベートファイルswffile;プライベートファイルdocfile; public docconverter(string filestring){ini(filestring); } / ** *リセットファイル * * @param fileString * / public void setFile(String FileString){ini(fileString); } / ** *初期化 * * @param filestring * / private void ini(string filestring){this.filestring = filestring; filename = filestring.substring(0、filestring.lastindexof( "。")); docfile = new file(filestring); pdffile = new file(filename + ".pdf"); swffile = new file(filename + ".swf"); } / ** * pdfに変換 * * @param file * / private void doc2pdf()throws exception {if(docfile.exists()){if(!pdffile.exists()){openofficeconnection connection = new socketopenofficeconection(8100); try {connection.connect(); documentConverter converter = new OpenOfficeDocumentConverter(connection); converter.convert(docfile、pdffile); //接続connection.disconnect(); System.out.println( "**** PDF変換が成功しました、pdf出力:" + pdffile.getPath() + "****"); } catch(java.net.connectexception e){e.printstacktrace(); System.out.println( "**** SWFコンバーターの例外、OpenOfficeサービスが開始されていません!****"); eを投げる; } catch(com.artofsolving.jodconverter.openoffice.connection.openofficeexception e){e.printstacktrace(); system.out.println( "**** SWFコンバーターの例外、変換ファイルの読み取り失敗****"); eを投げる; } catch(Exception e){e.printstacktrace(); eを投げる; }} else {system.out.println( "****がpdfに変換されました。変換は必要ありません****"); }} else {system.out.println( "**** SWFコンバーターの例外、変換する必要があるドキュメントは存在せず、変換することはできません****"); }} / ** * swfに変換 * / @suppresswarnings( "unused")private void pdf2swf()throws exception {runtime r = runtime.getRuntime(); if(!swffile.exists()){if(pdffile.exists()){if(Environment == 1){// Windows Environment Processing try {Process P = r.exec( "d:/swftools/pdf2wf.exe" + pdffile.getpath() + "-o" System.out.print(loadStream(p.getinputStream())); System.err.print(loadStream(p.geterrorStream())); System.out.print(loadStream(p.getinputStream())); System.err.println( "**** SWF変換は成功します、ファイル出力:" + swffile.getPath() + "****"); if(pdffile.exists()){pdffile.delete(); }} catch(ioexception e){e.printstacktrace(); eを投げる; }} else if(環境== 2){// linux環境処理try {process p = r.exec( "pdf2swf" + pdffile.getpath() + "-o" + swffile.getPath() + "-t 9"); System.out.print(loadStream(p.getinputStream())); System.err.print(loadStream(p.geterrorStream())); System.err.println( "**** SWF変換は成功します、ファイル出力:" + swffile.getPath() + "****"); if(pdffile.exists()){pdffile.delete(); }} catch(例外e){e.printstacktrace(); eを投げる; }}} else {system.out.println( "**** pdfは存在せず、変換できません****"); }} else {system.out.println( "**** swfはすでに存在し、****"); }} 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(); } / ***メインメソッドを変換* / @suppresswarnings( "unused")public boolean convert(){if(swffile.exists()){system.out.println( "**** swfコンバーターの動作を開始すると、ファイルはswf ****"); trueを返します。 } if(Environment == 1){System.out.Println( "**** SWFコンバーターが動作を開始し、現在の実行環境Windows ****"); } else {system.out.println( "**** swfコンバーターが動作を開始し、現在の実行環境Linux ****"); } try {doc2pdf(); pdf2swf(); } catch(Exception e){e.printstacktrace(); falseを返します。 } if(swffile.exists()){return true; } else {return false; }} / ** * return file path * * @param s * / public string getswpath(){if(swffile.exists()){string tempstring = swffile.getPath(); tempString = tempstring.replaceall( "////"、 "/"); TempStringを返します。 } else {return ""; }} / ***出力パスを設定* / public void setOutputPath(String OutputPath){this.OutputPath = outputPath; if(!outputpath.equals( "")){string realname = filename.substring(filename.lastindexof( "/")、filename.lastindexof( "。")); if(outputpath.charat(outputpath.length())== '/'){swffile = new file(outputpath + realname + ".swf"); } else {swffile = new file(outputpath + realname + ".swf"); }}}}6。展開とリリース
Tomcatを起動し、現在のWebアプリケーションを展開します
http:// localhost:8080/ctcesims/documentupload.jspを以下に示します。
[プレビュー]ボタンをクリックすると、下の図に示すように、プレビューインターフェイスが生成されます。
4.よくある質問
SWFが表示された場合は、プレビューできません。ご覧ください
http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04a.html#119065
SWFSが信頼ファイルの場所として生成されるフォルダーを設定します。
以下は、Flexpaper 2.1.9の違いを示しています。初期化方法が変更されます。ファイルディレクトリがプロジェクトディレクトリと一緒にない場合、添付ファイルディレクトリはサーバー内の仮想ディレクトリとして設定できます。
<%@ page Language = "Java" contentType = "text/html; charset = utf-8" pageencoding = "utf-8"%> <%// string swffilepath = session.getTribute( "swfpath")。 "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv =" content-type "content =" text/html; charset = utf-8 "> <script =" text/JavaScript " type = "text/javascript" src = "js/flexpaper.js"> </script> <script type = "text/javascript" src = "js/flexpaper_handlers.js"> </scrip> <style type = "text/css" media = "screen"> html、body:100%; } body {margin:0;パディング:0;オーバーフロー:自動; } #flashcontent {display:none; } </style> <title>ドキュメントオンラインプレビューシステム</title> </head> <body> <div style = "position:aspolute; left:50px; top:10px;"> <div id = "documentViewer"> </div> <script = "text/javascript"> var startdocument = "paper"; $('#documentViewer').FlexPaperViewer( { config : { SWFFile : 'upload/ddd3.swf', 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: 'portilem'、rendernordord: 'flash'、startAtpage: ''、viewmodetoolsvisible:true、zoomtoolsvisible:true、navtoolsvisible:true、cursortoolsvisible:true、seartootoolible: 'true、wmode:' ); </script> </div> </body> </html>最後に、印刷機能とロゴを削除する必要がある場合は、FlexpaperのFlashファイルを再コンパイルし、オンラインでダウンロードできます。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。