Learn Java Simulation implementation Baidu document online browsing _java

Source: Internet
Author: User
Tags stringbuffer

This idea is I refer to the Internet and come, the code is my realization.
Use the OpenOffice below Apache to convert the resource file into a PDF file, and 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)

After downloading, do not rush to install, please read this blog post

1, first look at our MYECLIPSE engineering structure

2, will we download the Jodconverter-2.2.2.zip after decompression will all the jar files copied to the Baidudoc under the Lib to

3, under the Webroot New Flexpaper folder, will be extracted after the Flexpaper all copied to 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 afternoon 2:24:58 * todo:1, First, get to the file you need to see * 2, Part II, the file to be obtained (doc,xls,txt,ppt,03/07 version To PDF, this step requires calling OpenOffice * 3, Part III, converting the resource file into a PDF file, converting it to a SWF file, using flexpaperviewer.swf for browsing View/public class Baiduservlet Exte NDS HttpservLet {private file sourcefile;//the source file to convert private file pdffile;//PDF Intermediate File object Private file swffile;//swf target file Object Priva Te string filepath;//is used to save the file path private String filename;//The filename that does not include the suffix name public file Getsourcefile () {return source
  File;
  The public void Setsourcefile (File sourcefile) {this.sourcefile = sourcefile;
  Public File Getpdffile () {return pdffile;
  The public void Setpdffile (File pdffile) {this.pdffile = Pdffile;
  Public File Getswffile () {return swffile;
  The 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 file objects sourcefile = new files (filePath);
    Pdffile = new File (FileName + ". pdf");
    Swffile = new File (FileName + ". swf");
    System.out.println (Pdffile);
    System.out.println (Swffile);
    1, convert the source file into PDF file Src2pdf ();
    try {//2, convert PDF file to swf file pdf2swf ();
    catch (Exception e) {e.printstacktrace ();
    ///Bind the converted file 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 (so Urcefile.exists ()) {//If not present, need to be transferred to PDF file if (!pdffile.exists ()) {//enable OpenOffice provided conversion service Openofficeconnection Co
        nn = new socketopenofficeconnection (8100);
          Connect OpenOffice Server try {conn.connect ();
          Create a file Converter object Documentconverter converter = new Openofficedocumentconverter (conn);
          Converter.convert (SourceFile, pdffile);
          Disconnect the link conn.disconnect ();
        SYSTEM.OUT.PRINTLN ("Conversion successful");
        catch (Connectexception e) {e.printstacktrace (); } else {System.out.println ("PDF file already exists, no need to convert!!")
      "); }} else {System.out.println ("file path does not exist!!!
    "); }/** * @Author: Nuoyan * @Date: 2015-2-2 pm 2:28:32 * @throws Exception * todo:pdf converted to SWF file/pri  vate void pdf2swf () throws Exception {if (!swffile.exists ()) {if (pdffile.exists ()) {String Command = "C:\\pdf2swf\\pdf2swf.exe" + pdffile.getpath () + "-O" + swffile.getpath () + "-T 9";
        SYSTEM.OUT.PRINTLN ("Conversion command:" + command);
        Java invokes an external command to convert the PDF to swf Runtime r = runtime.getruntime ();
        Process p = r.exec (command);
        System.out.println (Loadstream (P.getinputstream ())); System.out.println ("SWF file succeeds!!!
        ");
      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, the preparation of basefile.jsp documents

<%@ 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" >  

Notice the code in the basefile.jsp, no you can refer here

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

7, to be completed here, it is necessary to note that:
(1), Swftools-2013-04-09-1007.exe file installation path not too deep, otherwise Java invoke External command can not be executed

(2),

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

(3), start OpenOffice command, not directly double-click to start. The official website launches the way, uses the CD command to open installs the directory!
After installing OpenOffice
A. 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
B. See if the installation was successful
2.1 View the port corresponding to the PID
Netstat-ano|findstr "8100"
2.2 View the PID corresponding service program name
Tasklist|findstr "IPD Value"

Effect Diagram:

The above is the entire content of this article, I hope to help you learn.

Related Article

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.