บทความนี้ส่วนใหญ่แนะนำการใช้งาน Java ของตัวอย่างสิ่งที่แนบมาซึ่งต้องใช้ OpenOffice, SWFTools และ FlexPaper ขั้นตอนเฉพาะมีดังนี้:
1. ภาพรวม
หลักการหลัก
1. แปลง Word, Excel, PPT, TXT และไฟล์อื่น ๆ เป็นไฟล์ PDF ผ่านเครื่องมือของบุคคลที่สาม OpenOffice
2. แปลงไฟล์ PDF เป็นไฟล์รูปแบบ SWF ผ่าน SWFTOOLS
3. แสดงบนหน้าผ่านส่วนประกอบเอกสาร FlexPaper
2. ดาวน์โหลดแพ็คเกจการติดตั้ง
1.OpenOffice เป็นซอฟต์แวร์ประมวลผลคำที่เปิดกว้างและฟรีภายใต้ Apache
ดาวน์โหลดที่อยู่: Apache Oppenoffice เว็บไซต์อย่างเป็นทางการดาวน์โหลดเวอร์ชัน - 3.4.1 http://www.openoffice.org/zh-cn/download/
2. SWFTOOLS เป็นชุดของชุดเครื่องมือที่ใช้ในการประมวลผลไฟล์ SWF ของ Flash เราใช้เพื่อแปลงไฟล์ 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 = "ซ็อกเก็ต, โฮสต์ = 127.0.0.1, พอร์ต = 8100; urp;" -NofirstStartWizard
ให้ความสนใจกับ '-' หน้าคำสั่งสุดท้ายอย่าเขียนผิด! หากคุณไม่สามารถรับบริการโครงการไม่สามารถดำเนินการต่อได้
ภาพหน้าจอของบริการเปิดตัวเว็บไซต์อย่างเป็นทางการมีดังนี้:
ภาพหน้าจอท้องถิ่น:
3. กระบวนการพัฒนา
1. สร้างโครงการใหม่และคัดลอกโฟลเดอร์ JS ในไฟล์ FlexPaper (รวมถึง flexpaper_flash_debug.js, flexpaper_flash.js, jQuery.js, ไฟล์ JS ทั้งสามนี้เป็นปลั๊กอินสำหรับไฟล์ SWF ตัวอย่าง) คัดลอก FlexPaperViewer.swf ไปยังไดเรกทอรีรูทเว็บไซต์ (ไฟล์ส่วนใหญ่จะใช้เป็นเครื่องเล่นที่เล่นไฟล์ SWF บนหน้าเว็บ) โครงสร้างไดเรกทอรีมีดังนี้:
หมายเหตุ: คุณต้องสร้างโฟลเดอร์อัปโหลด
2. สร้าง fileupload.jsp
<%@ page language = "java" contentType = "ข้อความ/html; charset = utf-8" pageencoding = "utf-8"%> <! doctype html สาธารณะ "-// w3c // dtd html 4.01 transitional // en" <html> <head> <meta http-equiv = "content-type" content = "text/html; charset = utf-8"> <title> เอกสารดูตัวอย่างออนไลน์ #CE4614; FONT-SIZE: 0.9EM; TEXT-ALIGN: CENTRE;} #msg-box .logo {ขอบล่าง: 5px solid #ece5d9; มาร์จิ้น-ล่าง: 20px; padding-bottom: 10px;} #msg-box. {margin-top: 20px;} </style> </head> <body> <div id = "msg-box"> <form name = "form1" method = "post" enctype = "multipart/form-data" action = "docuploadconvertaction.jsp"> <div> โปรดอัปโหลดไฟล์ กระบวนการอาจใช้เวลาหลายนาที โปรดรอสักครู่ </div> <p> <name input = "file1" type = "file"> </p> <p> <input type = "subment" name = "submit" value = "upload"> </p> </form> </div> </body> </html> 3. สร้างหน้าการแปลง docuploadConvertAction.jsp
<%@page language = "java" contentType = "ข้อความ/html; charset = utf-8" pageencoding = "utf-8"%> <%@page import = "java.io.*"%> <%@page import = "java.util.enumeration" นำเข้า = "com.oreilly.servlet.multipart.defaultFilerenamePolicy"%> <%@หน้านำเข้า = "com.cectsims.util.docconverter"%> <%@page import = "com.cectsims.util.docconverter"%> ส่วนประกอบ COS ซึ่งสามารถแทนที่ด้วยการอัปโหลดคอมมอนส์-ฟิลด์ หลังจากอัปโหลดไฟล์แล้วให้บันทึกในโฟลเดอร์อัปโหลด // รับสตริงการอัพโหลดไฟล์ Savedirectory = Application.getRealPath ("/")+"อัปโหลด"; // พิมพ์ System.out.out.println (Savedirectory); // แต่ละไฟล์สูงสุด 50m int maxPostSize = 50 * 1024 * 1024; // ใช้กลยุทธ์การตั้งชื่อเริ่มต้นของ COS, เพิ่ม 1, 2, 3 หลังจากชื่อที่ซ้ำกัน ... หากคุณไม่ได้เพิ่ม DFP และชื่อที่ซ้ำกันมันจะเขียนทับค่า defaultFilerenamePolicy dfp = ใหม่ defaultFilerenamePolicy (); // การเข้ารหัสการตอบสนองคือ "UTF-8" และกลยุทธ์การแก้ไขปัญหาความขัดแย้งชื่อไฟล์เริ่มต้นใช้เพื่อใช้การอัปโหลด หากไม่มีการเพิ่มชื่อซ้ำ DFP จะมีการเขียนทับข้อมูลหลายส่วน Multipartrequest Multi = ใหม่ multipartrequest (คำขอ, Savedirectory, MaxPostSize, "UTF-8", DFP); // multipartrequest multi = new Multipartrequest (คำขอ, Savedirectory, MaxPostsize, "UTF-8"); // เอาท์พุทข้อมูลข้อเสนอแนะการแจงนับไฟล์ = multi.getFilenames (); ในขณะที่ (files.hasmoreElements ()) {system.err.println ("ccc"); string name = (string) files.nextelement (); ไฟล์ f = multi.getFile (ชื่อ); if (f! = null) {string filename = multi.getFilesystemName (ชื่อ); // รับส่วนขยายของสตริงไฟล์ที่อัปโหลด extName = filename.substring (filename.AlastIndexof (".")+1); // ไฟล์ฟูลพา ธ สตริง LastFileName = savedirectory + "//" + ชื่อไฟล์; // รับชื่อไฟล์ที่ต้องแปลงและแทนที่ '/' ในชื่อพา ธ ด้วย '/' สตริงคอนเวอร์ฟีนชื่อ = savedirectory.replaceall ("////////////-///-2ph"/")+"/"+ชื่อไฟล์; System.out.println (converfilename); // โทรไปที่คลาสการแปลง Docconverter และส่งผ่านไฟล์เพื่อแปลงเป็นตัวสร้างของคลาสนี้ Docconverter d = New Docconverter (Converfilename); // เรียกใช้วิธีการแปลงเพื่อเริ่มการแปลงก่อนอื่นเรียกใช้ doc2pdf () เพื่อแปลงไฟล์ Office เป็น PDF; จากนั้นดำเนินการ pdf2swf () เพื่อแปลง PDF เป็น SWF; d.conver (); // เซลล์ getSwfPath () วิธีการและพิมพ์ระบบพา ธ ไฟล์ SWF ที่แปลงแล้ว PRINTLN (D.GETSWFPATH ()); // สร้างเส้นทางสัมพัทธ์ของ SWF เพื่อส่งผ่านไปยังสตริงเครื่องเล่น FlexPaper 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 สาธารณะ "-// w3c // dtd html 4.01 transitional // en" "http://www.w3.org/tr/html4/loose.dtd"> <html> charset = utf-8 "> <title> แทรกชื่อที่นี่ </title> <style> ร่างกาย {margin-top: 100px; พื้นหลัง: #fff; Font-Family: Verdana, Tahoma;} A {color: #CE4614;} #msg-box {color: #CE4614; FONT-SIZE: 0.9EM; TEXT-ALIGN: CENTRE;} #msg-box .logo {ขอบล่าง: 5px solid #ece5d9; มาร์จิ้น-ล่าง: 20px; padding-bottom: 10px;} #msg-box. {margin-top: 20px;} </style> </head> <body> <div> <form name = "viewform" id = "form_swf" action = "documentView.jsp" method = "post"> <อินพุต type = 'submit' value = 'preview' สร้างหน้ามุมมอง DocumentView.jsp
<%@ page language = "java" contentType = "ข้อความ/html; charset = utf-8" pageencoding = "utf-8"%> <%string swffilepath = session.getAttribute ("swfpath"). toString (); %> <! doctype html สาธารณะ "-// w3c // dtd html 4.01 transitional // en" "http://www.w3.org/tr/html4/loose.dtd"> <html> charset = utf-8 "> <script type =" text/javaScript "src =" js/jQuery.js "> </script> <script type =" text/javascript "src =" js/flexpaper_flash.js "> </script> <script type =" text/javascript type = "text/css" media = "หน้าจอ"> html, body {ความสูง: 100%; } ร่างกาย {มาร์จิ้น: 0; Padding: 0; ล้น: อัตโนมัติ; } #flashContent {display: none; } </style> <title> เอกสารพรีวิวออนไลน์ระบบ </title> </head> <body> <div style = "ตำแหน่ง: สัมบูรณ์; ซ้าย: 50px; top: 10px;"> <a id = "ViewerPlaceholder"> </a> <script type = "ข้อความ/javascript" Escape ('<%= swffilepath%>'), มาตราส่วน: 0.6, ZoomTransition: 'Easeout', Zoomtime: 0.5, ZoomInterval: 0.2, FitPageOnload: จริง, fitWidThonload: False, FullScreenasmaxWindow: เท็จ 'SinglePage', ViewModetoolsVisible: จริง, 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.socketopenofficonnection; นำเข้า com.artofsolving.jodconverter.openoffice.converter.openofficedocumentconverter; / *** การแปลงรูปแบบ Doc Docx*/ คลาสสาธารณะ Docconverter {สภาพแวดล้อม int สุดท้ายคงที่ส่วนตัว = 1; // สภาพแวดล้อม 1: Windows 2: Linux Private String Fileestring; // (เกี่ยวข้องกับปัญหาพา ธ PDF2SWF) สตริงส่วนตัว = "" // พา ธ อินพุต ไฟล์ส่วนตัว pdffile; ไฟล์ส่วนตัว swffile; เอกสารไฟล์ส่วนตัว; Public Docconverter (String Filestring) {ini (filestring); } / ** * รีเซ็ตไฟล์ * * @param filestring * / public void setfile (สตริง filestring) {ini (filestring); } / ** * เริ่มต้น * * @param filestring * / โมฆะส่วนตัว ini (สตริง filestring) {this.filestring = filestring; filename = filestring.substring (0, filestring.lastindexof (".")); docFile = ไฟล์ใหม่ (filestring); pdffile = ไฟล์ใหม่ (ชื่อไฟล์ + ".pdf"); swffile = ไฟล์ใหม่ (ชื่อไฟล์ + ".swf"); } / ** * แปลงเป็น pdf * * @param ไฟล์ * / โมฆะส่วนตัว doc2pdf () โยนข้อยกเว้น {ถ้า (docfile.exists ()) {ถ้า (! ลอง {connection.connect (); DocumentConverter Converter = ใหม่ OpenFficEdocumentCumentConverter (การเชื่อมต่อ); Converter.Convert (docFile, pdffile); // ปิดการเชื่อมต่อการเชื่อมต่อ disconnect (); System.out.println ("**** การแปลง PDF สำเร็จเอาต์พุต PDF:" + pdffile.getPath () + "****"); } catch (java.net.connectException e) {e.printstacktrace (); System.out.println ("**** SWF Converter Exception, OpenOffice Service ไม่ได้เริ่มต้น! ****"); โยน 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 converter exception, เอกสารที่ต้องแปลงไม่มีอยู่และไม่สามารถแปลง ****"); }} / ** * แปลงเป็น swf * / @suppresswarnings ("ไม่ได้ใช้") โมฆะส่วนตัว pdf2swf () โยนข้อยกเว้น {runtime r = runtime.getRuntime (); if (! swffile.exists ()) {ถ้า (pdffile.exists ()) {ถ้า (สภาพแวดล้อม == 1) {// การประมวลผลสภาพแวดล้อมของ Windows ลอง {process p = r.exec ("d:/ไฟล์โปรแกรม/swftools/pdf2swf.exe" + pdffile.getpath () 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; }} อื่นถ้า (สภาพแวดล้อม == 2) {// การประมวลผลสภาพแวดล้อม linux ลอง {กระบวนการ 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 (exception e) {e.printstacktrace (); โยน e; }}} else {system.out.println ("**** pdf ไม่มีอยู่ไม่สามารถแปลงได้ ****"); }} else {system.out.println ("**** swf มีอยู่แล้วและไม่จำเป็นต้องแปลง ****"); }} สตริงคงที่ loadstream (inputstream ใน) พ่น ioexception {int ptr = 0; ใน = ใหม่ bufferedInputStream (in); StringBuffer buffer = new StringBuffer (); ในขณะที่ ((ptr = in.read ())! = -1) {buffer.append ((char) ptr); } return buffer.toString (); } / ***แปลงวิธีการหลัก* / @suppresswarnings ("ไม่ได้ใช้") บูลีนสาธารณะแปลง () {ถ้า (swffile.exists ()) {system.out.println ("**** ตัวแปลง SWF เริ่มทำงานไฟล์ถูกแปลงเป็น swf ****"); กลับมาจริง; } if (environment == 1) {system.out.println ("**** SWF Converter เริ่มทำงาน, สภาพแวดล้อมที่กำลังทำงานอยู่ปัจจุบัน windows ****"); } else {system.out.println ("**** ตัวแปลง SWF เริ่มทำงานสภาพแวดล้อมการรันปัจจุบัน linux ****"); } ลอง {doc2pdf (); pdf2swf (); } catch (exception e) {e.printstacktrace (); กลับเท็จ; } if (swffile.exists ()) {return true; } else {return false; }} / ** * ส่งคืนเส้นทางไฟล์ * * @param S * / สตริงสาธารณะ getSwfPath () {ถ้า (swffile.exists ()) {String tempString = swffile.getPath (); tempstring = tempstring.replaceall ("///////", "/"); กลับมาอุณหภูมิ; } else {return ""; }} / *** ตั้งค่าเส้นทางเอาต์พุต* / โมฆะสาธารณะ setOutputPath (สตริงเอาท์พุททิท) {this.outputPath = outputPath; if (! outputPath.equals ("")) {string realname = filename.substring (filename.lastindexof ("/"), filename.lastindexof ("."); if (outputPath.Charat (outputPath.length ()) == '/') {swffile = ไฟล์ใหม่ (outputPath + realname + ".swf"); } else {swffile = ไฟล์ใหม่ (outputPath + realname + ".swf"); }}}}6. การปรับใช้และการเปิดตัว
เริ่ม Tomcat และปรับใช้เว็บแอปพลิเคชันปัจจุบัน
ป้อน http: // localhost: 8080/ctcesims/documentupload.jsp ในแถบที่อยู่ดังแสดงด้านล่าง:
คลิกปุ่มดูตัวอย่างและอินเทอร์เฟซตัวอย่างจะถูกสร้างขึ้นดังแสดงในรูปด้านล่าง:
4. คำถามที่พบบ่อย
หาก SWF ปรากฏขึ้นคุณไม่สามารถดูตัวอย่างได้โปรดเยี่ยมชม
http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04a.html#119065
ตั้งค่าโฟลเดอร์ที่ SWF ถูกสร้างขึ้นเป็นตำแหน่งไฟล์ Trust
ต่อไปนี้ให้ความแตกต่างระหว่าง FlexPaper 2.1.9: วิธีการเริ่มต้นมีการเปลี่ยนแปลง หากไดเรกทอรีไฟล์ไม่ได้อยู่ร่วมกับไดเรกทอรีโครงการไดเรกทอรีไฟล์แนบสามารถตั้งค่าเป็นไดเรกทอรีเสมือนจริงในเซิร์ฟเวอร์
<%@ page language = "java" contentType = "ข้อความ/html; charset = utf-8" pageencoding = "utf-8"%> <%// สตริง swffilepath = session.getattribute ("swfpath") toString ();%> <! "http://www.w3.org/tr/html4/loose.dtd"><html> <head> <meta http-equiv =" content-ype "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"> </script> <style type = "ข้อความ/css" media = "หน้าจอ" } ร่างกาย {มาร์จิ้น: 0; Padding: 0; ล้น: อัตโนมัติ; } #flashContent {display: none; } </style> <title> เอกสารพรีวิวออนไลน์ระบบ </title> </head> <body> <div style = "ตำแหน่ง: Absolute; left: 50px; top: 10px;"> <div id = "documentViewer"> </div> <script type = "JavaScript"> var startDocument = "Paper"; $ ('#documentViewer'). FlexPaperViewer ({config: {swffile: 'upload/ddd3.swf', มาตราส่วน: 0.6, ZoomTransition: 'Easeout', Zoomtime: 0.5, Zoominterval: 0.2, FitPageOnload: True MaxZoomsize: 5, SearchMatchall: FALSE, InitViewMode: 'Portrait', RenderingOrder: 'Flash', StartAtPage: '', ViewModetoolsVisible: จริง, zoomtoolsvisible: true, navtoolsvisible: true, cursortoolsvisible - </script> </div> </body> </html> สุดท้ายหากคุณต้องการลบฟังก์ชั่นการพิมพ์และโลโก้คุณสามารถคอมไพล์ไฟล์แฟลชของ FlexPaper อีกครั้งและดาวน์โหลดออนไลน์
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น