Java simulation Implementation of Baidu document online browsing

Source: Internet
Author: User


Java Simulation Implementation of Baidu document online browsing

This idea is my reference to the Internet, code is my implementation.
Use the OpenOffice below Apache to convert the resource file into a PDF file, then convert the PDF file into a SWF file and browse with Flexpaper.
Ok
A, download OpenOffice (convert resource file)
B. Download Jodconverter (call OpenOffice)
C, download Swftools (pdf2swf)
D. Download Flexpaper (browse swf file)


Here I have all downloaded, all you need to download: http://download.csdn.net/detail/u010506940/8418553


After the download, do not hurry to install, please read this blog post

1, First look at our MYECLIPSE engineering structure




2 . Download the extracted files and then copy all the jars to the Baidudoc lib.




3 . Create a new folder under Webroot and copy all the extracted copies into the Flexpaper.


4. New Baiduservlet.java file

Package Com.baidu.util;import Java.io.bufferedinputstream;import Java.io.file;import java.io.FileInputStream; Import Java.io.ioexception;import Java.io.inputstream;import Java.net.connectexception;import Javax.imageio.stream.fileimageinputstream;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;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;/** * @Author: NuoYan * @Date       : 2015-2-2 PM 2:24:58 * todo:1, first step, first get to the file you need to view * 2, the second part, the obtained file (doc,xls,txt,ppt,03/07 version into pdf), this step needs to call OpenOffice * 3, the third part, convert the resource file converted PDF file to SWF file, use flexpaperviewer.swf to browse and view */public class Baiduservlet extends HttpServlet {private Fil E Sourcefile;//source file to convert private file pdffile;//PDF Intermediate File object Private file swffile;//swf target file object private String filepath;// The file path used to save the private String filename;//does not include the suffix name of public file Getsourcefile () {return sourcefile;} public void Setsourcefile (File sourcefile) {this.sourcefile = sourcefile;} Public File Getpdffile () {return pdffile;} public void Setpdffile (File pdffile) {this.pdffile = Pdffile;} Public File Getswffile () {return swffile;} public void Setswffile (File swffile) {this.swffile = Swffile;} Public String GetFilePath () {return filePath;} public void SetFilePath (String filePath) {this.filepath = FilePath;} Public String GetFileName () {return fileName;} public void Setfilename (String fileName) {this.filename = FileName;} public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { String savefilename = Request.getparameter ("Savfile"); System.out.println (Savefilename); String Webpath = Request.getrealpath ("/"); FilePath = Webpath + "reader\\" + Savefilename;fiLename = filepath.substring (0, Filepath.lastindexof (".")); /create three files Object sourcefile = new file (filePath);pd ffile = new file (filename + ". pdf"), Swffile = new file (filename + ". swf"); System.out.println (Pdffile); System.out.println (swffile);//1. Convert source file to PDF file Src2pdf (), try {//2, convert PDF file to swf file pdf2swf ();} catch (Exception e) { E.printstacktrace ();} Bind the converted files to the session request.getsession (). SetAttribute ("Swfname", Swffile.getname ()); System.out.println (Swffile.getname ());//redirect to preview page Response.sendredirect (request.getcontextpath () + "/reader/ Basefile.jsp ");} /** * @Author: Nuoyan * @Date: 2015-2-2 pm 2:28:22 todo://source file converted to PDF file */private void Src2pdf () {if (sourcefile.exists ()) {// If not present, need to be transferred to PDF file if (!pdffile.exists ()) {//enable OpenOffice provided conversion service Openofficeconnection conn = new Socketopenofficeconnection (8100);//Connect OpenOffice Server try {conn.connect ();//Create File Converter object Documentconverter converter = new Openofficedocumentconverter (conn); Converter.convert (SourceFile, pdffile);//Break link conn.disconnect (); System.out.println ("Conversion succeeded ");} catch (Connectexception e) {e.printstacktrace ();}} else {System.out.println ("already exists PDF file, does not need to be converted!! ");}} else {System.out.println ("file path does not exist!!! ");}} /** * @Author: Nuoyan * @Date: 2015-2-2 pm 2:28:32 * @throws Exception * todo:pdf convert to swf file */private void pdf2swf () throws Exc  eption {if (!swffile.exists ()) {if (pdffile.exists ()) {String command = "C:\\pdf2swf\\pdf2swf.exe" + pdffile.getpath () + " -O "+ swffile.getpath () +"-T 9 "; System.out.println ("Convert command:" + command);//Java Calls external command, performs PDF conversion to swfruntime r = Runtime.getruntime (); Process p = r.exec (command); System.out.println (Loadstream (P.getinputstream ())); System.out.println ("SWF file is successfully transferred!!! "); System.out.println (Swffile.getpath ());} else {System.out.println ("No PDF file exists");}}} private static String Loadstream (InputStream in) throws Exception {int len = 0;in = new Bufferedinputstream (in); StringBuffer buffer = new StringBuffer (), while (len = In.read ())! =-1) {buffer.append ((char) len);} return buffer.tostring ();}}

5.Modify Index.jsp

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >


6.Writing basefile.jsp files

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
note The code in the basefile.jsp, not you can refer to here



/**************************************************************************************/

7. Precautions

1, This file installation path not too deep, lest Java call External command, cannot execute, I here is C disk under the Pdf2swf folder



2.

2.1, Red 1 mark the path can not be wrong, I made this mistake
2.2, Red Flag 2 can also write http://127.0.0.1:8080/baiduDoc/reader/ ...


3, start the OpenOffice command, not directly double-click to start. The official website launches the following way:


After installing OpenOffice, enter the command interface->CD into the installation directory, as follows:
1. Installation Services
CD C:\Program Files (x86) \openoffice4\program
This step you can see where your OpenOffice is installed
Perform
Soffice-headless-accept= "SOCKET,HOST=127.0.0.1,PORT=8100;URP;"-nofirststartwizard
2. See if the installation was successful
2.1 Viewing the PID of the port
Netstat-ano|findstr "8100"
2.2 View the PID corresponding service program name
Tasklist|findstr "IPD Value"
Shown


Source Address : http://download.csdn.net/detail/u010506940/8418561


If you have any questions, please leave a message to find me! Ok? Don't idle my resources expensive oh, haha ~


Java simulation Implementation of Baidu document online browsing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.