Advance to Office Web Apps consolidating Java Web projects

Source: Internet
Author: User

The previous two articles to install the server, the main project is so that its integration into our projects, the Internet is mostly ASP, there is little introduction of how Java integration, after Baidu, and finally integrated into my project.

First, build a servlet interceptor.

public void DoFilter (ServletRequest request, servletresponse response, Filterchain chain) throws IOException, servletexception {HttpServletRequest HttpRequest = (httpservletrequest) request; HttpServletResponse HttpResponse = (httpservletresponse) response; String uri = Httprequest.getrequesturi (); wopihost/wopi/files/excel.xlsx//solve Chinese garbled problem string Fileuri = Urldecoder.decode (uri.substring (Uri.indexof ("/WOPI/") + 1, uri.length ()), "UTF-8"); /wopi/files/test.docx//string FilePath = Request.getservletcontext (). Getrealpath ("/") + Fileuri; String FilePath = "d:\\upload\\oa\\" + fileuri;if (Fileuri.endswith ("/contents")) {//GetFile: Return file stream filePath = Filepath.s Ubstring (0, Filepath.indexof ("/contents")); GetFile (FilePath, HttpResponse);} else {//Checkfileinfo: Return jsonresponse.setcharacterencoding ("UTF-8"); Response.setcontenttype ("Application/json; Charset=utf-8 "); PrintWriter out = null;try {out = Response.getwriter (); Out.write (Fileutil.checkfileinfo (FilePath));} catch ( IOException e) {E.prinTstacktrace ();} Finally {if (out! = null) {Out.close ();}}} return;} Private HttpServletResponse GetFile (String path, httpservletresponse response) {try {//path is the path to the file you want to download. File File = new file (path);//Gets the file name. String filename = File.getname (); String ContentType = "Application/octet-stream";//Download the file in the form of a stream. InputStream fis = new Bufferedinputstream (new FileInputStream (path)); byte[] buffer = new byte[fis.available ()];fis.read (buffer); Fis.close ();//Empty Responseresponse.reset ();//Set Response Headerresponse.addheader ("Content-disposition", "Attachment;filename=" + New String (Filename.getbytes ("Utf-8"), "iso-8859-1"), Response.AddHeader ("Content-length") , "" + file.length ()), outputstream toclient = new Bufferedoutputstream (Response.getoutputstream ()); Response.setcontenttype (ContentType); toclient.write (buffer); Toclient.flush (); Toclient.close ();} catch (IOException ex) {ex.printstacktrace ();} return response;}

The tool class Fileutil code is as follows:

/** * Get file basic information *  * @param filePath file path * @return */public static string Checkfileinfo (String filePath) {File File = NE W File (FilePath); String basefilename = null; File name string ownerid = null; Unique number of the file owner long size = 0; File size, in bytes units//String sha256 = null; The SHA-2 encoded hash content of the 256 bit of the file is long Version = 0; File version number, if the file is edited, the version number is also changed if (File.exists ()) {//Gets the file name. Basefilename = File.getname (); size = File.length ();//Gets the file's suffix name. String ext = basefilename.substring (Basefilename.lastindexof (".") + 1); ownerid = "admin";//sha256 = new Shautils (). SHA (fileutils.readbybyte (file),//"SHA-256"); version = File.lastmodified ();} Return "{\" basefilename\ ": \" "+ Basefilename +" \ ", \" ownerid\ ": \" "+ ownerID +" \ ", \" size\ ": \" "+ size+" \ ", \" Allowexterna Lmarketplace\ ": \" "+ True +" \ ", \" version\ ": \" "+ Version +" \ "}";}

Before the installation of the test when the Xml:http://docview.mingdao.com/hosting/discovery open, different formats of the document call different, specific can be seen in detail.

Access http://127.0.0.1:8080/xxxx/wopi/files/test.docx/contents Download the file
Visit http://xxx.docview.com/wv/wordviewerframe.aspx? wopisrc=http://xxx.xxx.com/blog/http%3a%2f%2f192.168.1.11%3a8080%2fwopihost%2fwopi%2ffiles%2ftest.docx to preview

Previous online queries were fragmented, and today the Java Web Consolidation Office Web Apps is centralized from deployment to consolidation to Web projects.

Previous post: Building Office Web Apps Server http://www.cnblogs.com/gkl2013/p/5667959.html

Advance to Office Web Apps consolidating Java Web projects

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.