Java simulates the online browsing of Baidu documents, and java simulates Baidu documents

Source: Internet
Author: User

Java simulates the online browsing of Baidu documents, and java simulates Baidu documents


Java simulates the online browsing of Baidu documents

The idea is that I come from the Internet and the code is my implementation.
Use OpenOffice under Apache to convert the resource file into a PDF file, convert the PDF file into a swf file, and use FlexPaper to browse.
OK,
A. Download OpenOffice (convert resource files)
B. Download JodConverter (call OpenOffice)
C. Download Swftools (ipv2swf)
D. Download FlexPaper (Browse swf files)


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


After the download, do not worry about installation. Please read this blog post.

1. First look at our MyEclipse project structure




2. decompress the downloaded package and copy all jar files to the lib of baiduDoc.




3. Create a folder under WebRoot and copy all the decompressed files to FlexPaper.


4. Create a 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. httpServl EtResponse; 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: 2:24:58 * TODO: 1. First, obtain the file to be viewed., Convert the obtained files (doc, xls, txt, ppt, and 03/07 to PDF ), in this step, you need to call OpenOffice * 3rd, convert the source File converted from the resource File to the SWF File, and use flexpaperviewer.swf to browse and view */public class BaiDuServlet extends HttpServlet {private File sourceFile; // The source File to be converted is; // pdf intermediate File object private File swfFile; // swf target File object private String filePath; // used to save the File path private String fileName; // The File name without the suffix public File getSourceFile () {return sourceFile;} public void set SourceFile (File sourceFile) {this. sourceFile = sourceFile;} public File getPdfFile () {return pdfFile;} public void setPdfFile (File pdfFile) implements this.pdf File = pdfFile;} public File getSwfFile () {return swfFile ;} public void setSwfFile (File swfFile) Export this.swf File = swfFile;} public String getFilePath () {return filePath;} public void setFilePath (String filePath) {this. filePath = filePath;} public String getFi LeName () {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 = fileP Ath. substring (0, filePath. lastIndexOf (". "); // create three File objects sourceFile = new File (filePath); pdfFile = new File (fileName + ". pdf "); swfFile = new File (fileName + ". swf "); System. out. println (pdfFile); System. out. println (swfFile); // 1. Convert the source file to a pdf file src2pdf (); try {// 2. Convert the pdf file to a swf file %2swf ();} catch (Exception e) {e. printStackTrace ();} // bind the converted file to the session for request. getSession (). setAttribute ("swfName", swfFile. GetName (); System. out. println (swfFile. getName (); // redirect to the preview page response. sendRedirect (request. getContextPath () + "/reader/baseFile. jsp ");}/*** @ Author: NuoYan * @ Date: 2:28:22 TODO: // convert the source file to a PDF file */private void src2pdf () {if (sourceFile. exists () {// if it does not exist, convert it to a PDF file if (! PdfFile. exists () {// enable OpenOfficeConnection conn = new SocketOpenOfficeConnection (8100) provided by OpenOffice; // connect to OpenOffice server try {conn. connect (); // create the File converter object DocumentConverter converter = new OpenOfficeDocumentConverter (conn); converter. convert (sourceFile, pdfFile); // disconnect conn. disconnect (); System. out. println ("converted successfully");} catch (ConnectException e) {e. printStackTrace () ;}} else {System. out. println ("PDF already exists File, which does not need to be converted !! ") ;}} Else {System. out. println (" the file path does not exist !!! ") ;}}/*** @ Author: NuoYan * @ Date: 2:28:32 * @ throws Exception * TODO: Convert PDF to SWF file */private void %2swf () throws Exception {if (! SwfFile. exists () {if (pdfFile. exists () {String command = "C :\\%2swf \ %2swf.exe" + pdfFile. getPath () + "-o" + swfFile. getPath () + "-T 9"; System. out. println ("conversion command:" + command); // Java calls an external command and executes the pdf to convert it to swfRuntime r = Runtime. getRuntime (); Process p = r.exe c (command); System. out. println (loadStream (p. getInputStream (); System. out. println ("swf file copy succeeded !!! "); System. out. println (swfFile. getPath ();} else {System. out. println ("pdf file does not exist") ;}} 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. Compile the baseFile. jsp file

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

Pay attention to the Code in baseFile. jsp.



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

7. Notes

1. The installation path of this file should not be too deep, so that Java can call external commands and cannot execute them. Here I am the 2 SWF folder under drive C.



2,

2.1. the path marked in red 1 cannot be wrong. I made this mistake.
2.2 red mark 2 can also write http: // 127.0.0.1: 8080/baiduDoc/reader /...


3. The command to start OpenOffice is not directly started by double-clicking. The official website can be started as follows:


After the openoffice is installed, go to the command interface> cd to enter the installation directory, as shown below:
1. Install the service
Cd C: \ Program Files (x86) \ OpenOffice4 \ program
In this step, you can see where your OpenOffice is installed
Run
Soffice-headless-accept = "socket, host = 127.0.0.1, port = 8100; urp;"-nofirststartwizard
2. Check whether the installation is successful.
2.1 view the pid corresponding to the port
Netstat-ano | findstr" 8100"
2.2 view the service program name corresponding to the pid
Tasklist | findstr "ipd value"
Description:


Source Code address: Http://download.csdn.net/detail/u010506940/8418561


If you have any questions, please leave a message to me! OK? Don't be idle. My resources are expensive! Haha ~


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.