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