A preview of the Java implementation file

Source: Internet
Author: User

Recently the project needs to use the file preview function, began to collect the data on the net, has learned several kinds of file preview method. I have integrated the following three kinds of tests into my project, and the last one is using the third type:

    • Direct use of services provided by others such as: Office Web 365
    • Using the Openoffice+swftools+flexpaper implementation
    • Using the Openoffice+pdf.js implementation
1. Use the Office Web 365 third-party service to access the website;

Characteristics:

    1. The implementation is simple, simple can no longer be simple, you just need to provide it with a file resource link on it.
    2. It supports a variety of file types of previews, and can save the original file of various dynamic effects, such as PPT animation effect, and even compressed package can be previewed. In other words, you preview the file on this and you open it directly on the computer without any difference.
    3. Can realize the file cache, reduce the pressure of your server.
    4. There are free version, standard version, Advanced edition and other versions, different versions of the service is not the same, the free version of the preview file size can not exceed 5MB, the number of times per day preview (number of calls to the interface) can not exceed 500, for the high-demand system must use the paid version.
    5. Security issues, although the Office Web provides a high level of security, it's always a feeling that putting your stuff in someone else is not too safe.
    6. Not suitable for projects within the LAN.

The OpenOffice example is as follows:

No need to deploy, no programming, no need to do anything, just a link: Point me preview, link to:
Http://officeweb365.com/o/?i= your site id&furl= The Office file you want to preview
Cases:

1 No need to deploy, no programming, no need to do anything, just a link: Point me preview, link to: 2 http://officeweb365.com/o/?i= your website ID&furl3 Example:<   href= "Http://officeweb365.com/o/?i=1&furl=http://a.com/downfile/a.doc"  target  = "_blank"> point I preview </a>
2. Using Openoffice+swftools+flexpaper to implement

The main idea of using Openoffice+swftools+flexpaper to implement file preview is to convert other file types to PDF by calling the software OpenOffice installed on the server through a Java program. Then call Swftools to convert the PDF to a SWF file and preview the SWF file through the Flexpaper plugin on the front end.

Characteristics:

    1. This is a relatively complex implementation, requiring the installation of OpenOffice and Swftools software on the server, and then invoking the software in the program to complete the conversion of the file type.
    2. If you do not support Chinese path and Chinese file name, to support Chinese file names, you need to decompile flexpaper and then ... It's a hassle anyway.
    3. This is a lot of information, so you can find a lot of tutorials online to help you complete the configuration.

The specific implementation can refer to the following blog: openoffice+swftools+flexpaper implementation file preview

3. Using Openoffice+pdf.js

This method is used in my project, pdf.js display is better, and can be completed according to their own project needs of the relevant configuration. Here's what I use in my project:

1. Install OpenOffice First, start the service on the command line after the installation is successful, such as: Enter the program directory under the installation directory, and then execute the command: soffice-headless-accept= "socket,host=127.0.0.1, Port=8100;urp; "

See if there are any soffice.bin in the running process, and some of them start successfully.

2. Then import the following jar packages within the project:
Jodconverter-2.2.2.jar
Ridl-3.2.1.jar
Commons.io.jar
Juh.jar
Jurt.jar
Unoil.jar
Slf4j-api-1.7.13.jar Slf4j-jdk14-1.7.13.jar Xstream-1.4.1.jar
3. Write Java code to complete the conversion of other files to PDF: Direct initialization will pass the source file path and the file path of the PDF to be converted, the code is as follows
1  PackageCom.ams.util.server;2 3 ImportJava.io.BufferedInputStream;4 ImportJava.io.BufferedOutputStream;5 ImportJava.io.BufferedReader;6 ImportJava.io.File;7 ImportJava.io.FileInputStream;8 ImportJava.io.FileOutputStream;9 Importjava.io.IOException;Ten ImportJava.io.InputStream; One ImportJava.io.InputStreamReader; A  - ImportCom.artofsolving.jodconverter.DocumentConverter; - Importcom.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection; the Importcom.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection; - ImportCom.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter; -  -  Public classDocconverter { +      -     PrivateString FileName; +     PrivateFile Pdffile; A     PrivateFile docfile; at     PrivateFile Odtfile; -      -      -  -      PublicDocconverter (String sourcefile,string targetfile)throwsException { -  inFileName = sourcefile.substring (0, Sourcefile.lastindexof ("/"));  -Docfile =NewFile (sourcefile);//get the file before conversion to                  //get the file with the name without the suffix s +String s = sourcefile.substring (Sourcefile.lastindexof ("/") + 1,sourcefile.lastindexof (".")));  -filename = filename + "/" +s;  the              //used to process txt document converted to PDF format garbled, get the name of the uploaded file (do not need the following format _ *String txtname = sourcefile.substring (Sourcefile.lastindexof ("."));//Get file Format $              //determine if the uploaded file is a txt filePanax Notoginseng              if(Txtname.equalsignorecase (". txt")) {     -                  //txt is required to be converted to the ODT and then converted to PDF theOdtfile =NewFile (FileName + ". Odt"));  +                  //Copy the uploaded document back to 丿 and modify it to the ODT format, then the ODT format into PDF format A                   This. CopyFile (Docfile, odtfile);  thePdffile =NewFile (targetfile);//for working with PDF documents +}Else if(Txtname.equals (". pdf") | | txtname.equals (". PDF ")) {     -Pdffile =NewFile (targetfile);  $                   This. CopyFile (Docfile, pdffile);  $}Else{     -Pdffile =NewFile (targetfile);  -              }     theDoc2pdf ();//calling functions for conversion -      }    Wuyi     Private voidCopyFile (File sourcefile,file targetfile)throwsexception{ the         //Create a new file input stream and buffer it -FileInputStream input =NewFileInputStream (sourcefile); WuBufferedinputstream Inbuff =Newbufferedinputstream (input); -         //Create a new file output stream and buffer it AboutFileOutputStream output =NewFileOutputStream (targetfile); $Bufferedoutputstream Outbuff =Newbufferedoutputstream (output); -         //Buffer Array -         byte[]B =New byte[1024 * 5]; -         intLen; A          while(len = Inbuff.read (b))! =-1){ +Outbuff.write (b,0, Len); the         } -         //refreshes the output of this buffer Pei $ Outbuff.flush (); the         //Close Pei the inbuff.close (); the outbuff.close (); the output.close (); - input.close (); in     } the     Private voidDoc2pdf ()throwsException { the         if(Docfile.exists ()) { About             if(!pdffile.exists ()) { theOpenofficeconnection connection =NewSocketopenofficeconnection (8100); the                 Try { the Connection.connect (); +Documentconverter converter =NewOpenofficedocumentconverter (connection); - Converter.convert (Docfile, pdffile); the                     //Close the connectionBayi Connection.disconnect (); theSystem.out.println ("****pdf conversion succeeded, PDF output _" + Pdffile.getpath () + "* * *"); the}Catch(java.net.ConnectException e) { -                     //ToDo auto-generated Catch block - e.printstacktrace (); theSystem.out.println ("****swf conversion exception, OpenOffice Service not started! ****"); the                     Throwe; the}Catch(com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) { the e.printstacktrace (); -System.out.println ("****swf converter exception, read conversion file failed * * * * *"); the                     Throwe; the}Catch(Exception e) { the e.printstacktrace ();94                     Throwe; the                 } the}Else { theSYSTEM.OUT.PRINTLN ("* * * has been converted to PDF, no 霿 to convert * * * * * * * * *));98             } About}Else { -System.out.println ("****swf Converter exception, 霿 document to be converted does not exist, cannot convert * * * * *");101         }102     }
103}
4. The front end needs to import the Pdf.js plug-in, and then dynamically add the returned results to the Default_url in Viewer.js, where you can configure a variety of realistic effects.

The above is my implementation of several methods of file preview, and the vast number of netizens hope to learn and progress together! fighting!

A preview of the Java implementation file

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.