1, add maven dependence
<dependency>
<groupId>com.github.icecooly</groupId>
<artifactId>FastHttpClient</artifactId>
<version>1.7</version>
</dependency>
2, FastHttpClient example
https://gitee.com/icecooly/FastHttpClient
3, disposed in Jfinal of Start.java
/** * 配置路由 */ public void configRoute(Routes me) { me.add("/baseService/wopiImpl", WopiController.class); }
4、WopiController.java
package com.demo.blog; import com.jfinal.aop.Before; import com.jfinal.core.Controller; import com.jfinal.core.NotAction; import com.jfinal.ext.interceptor.GET; import com.jfinal.ext.interceptor.POST; import com.jfinal.upload.UploadFile; import java.io.*; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Base64; public class WopiController extends Controller { private String filePath = "D:\\Work\\"; / ** * Function: with wopi agreement to provide file download * Author: Yellow Sea * Time: 2019-08-17 * Use Case: http://127.0.0.1/baseService/wopi/files/123.docx/contents * / @ the Before ({. the GET class }) public void downloads () { String filename .. = getRequest () getRequestURI () Split ( "/") [. 4 ]; RenderFile ( new new File (filePath + filename)); } / ** * function: with wopi agreement, access to file information * author: Yellow Sea * time: 2019-08-17 * use Case: http://127.0.0.1/baseService/wopi/files/123.docx * / @Before ({gET . class}) public void getInfo() { String filename = getRequest().getRequestURI().split("/")[4]; FileInfo info = new FileInfo(); try { if (filename != null && filename.length() > 0) { File file = new File(filePath + filename); if (file.exists()) { info.setBaseFileName(file.getName()); info.setSize(file.length()); info.setOwnerId("admin"); info.setVersion (file.lastModified ()); info.setSha256 (getHash256 (File)); } } renderJson (info); } the catch (Exception E) { e.printStackTrace (); } } / ** * Function: with wopi protocol, to save the modified file * oF: Yellow Sea * time: 2019-08-17 * / @Before ({the pOST. class }) public void save () throws IOException { UploadFile UF = the getFile ( "Content" ) ; File upfile = new new File(filePath+uf.getFileName()); if (!upFile.getParentFile().exists()) { upFile.getParentFile().mkdirs(); } if(upFile.exists()) { upFile.delete(); } uf.getFile().renameTo(upFile); renderNull(); } /** * 获取文件的SHA-256值 * * @param file * @return */ @NotAction private String getHash256(File file) throws IOException, NoSuchAlgorithmException { String value = "" ; The try (FIS = the InputStream new new the FileInputStream (File)) { byte [] Buffer = new new byte [1024 ]; int numRead; // Returns the specified digest algorithm MessageDigest object MessageDigest digest = MessageDigest.getInstance ( "SHA- 256 " ); do { numRead = fis.read (Buffer); IF (numRead> 0 ) { // update summary digest.update (Buffer, 0 , numRead); } }while (numRead != -1); value = new String(Base64.getEncoder().encode(digest.digest())); } return value; } }
5、FileInfo.java
Package Penalty for com.demo.blog; Import the java.io.Serializable; / ** * Object File Properties * * Due to non-compliance with wopi interface hump naming rules, you need to specify an alias used @JsonProperty * the Created by ethendev ON 2017/4/15 . * / public class FileInfo the implements Serializable { / ** * filename * / Private String BaseFileName; / ** * unique number of the file owner * / Private String ownerId; / ** * file size, in bytes * / Private Long size; / ** * file 256 bit bit SHA-2 hash coding content * / Private String SHA256; / ** * file version number, if the document is edited, the version number must change * / Private Long Version; / ** * allow external service connection * / Private boolean allowExternalMarketplace = to true ; / ** * Change the file permissions * / Private Boolean userCanWrite = to true ; / ** * support update * / Private Boolean supportsUpdate = to true ; / ** * supports locking * / Private Boolean supportsLocks = to true ; public String getBaseFileName () { return baseFileName; } public void setBaseFileName(String baseFileName) { this.baseFileName = baseFileName; } public String getOwnerId() { return ownerId; } public void setOwnerId(String ownerId) { this.ownerId = ownerId; } public long getSize() { return size; } public void setSize(long size) { this.size = size; } public String getSha256() { return sha256; } public void setSha256(String sha256) { this.sha256 = sha256; } public long getVersion() { return version; } public void setVersion(long version) { this.version = version; } public boolean isAllowExternalMarketplace() { return allowExternalMarketplace; } public void setAllowExternalMarketplace(boolean allowExternalMarketplace) { this.allowExternalMarketplace = allowExternalMarketplace; } public boolean isUserCanWrite() { return userCanWrite; } public void setUserCanWrite(boolean userCanWrite) { this.userCanWrite = userCanWrite; } public boolean isSupportsUpdate() { return supportsUpdate; } public void setSupportsUpdate(boolean supportsUpdate) { this.supportsUpdate = supportsUpdate; } public boolean isSupportsLocks() { return supportsLocks; } public void setSupportsLocks(boolean supportsLocks) { this.supportsLocks = supportsLocks; } @Override public String toString() { return "FileInfo{" + "baseFileName='" + baseFileName + '\'' + ", ownerId='" + ownerId + '\'' + ", size=" + size + ", sha256='" + sha256 + '\'' + ", version=" + version + ", allowExternalMarketplace=" + allowExternalMarketplace + ", userCanWrite=" + userCanWrite + ", supportsUpdate=" + supportsUpdate + ", supportsLocks=" + supportsLocks + '}'; } }
6, the test class
Package com.demo.blog; Import io.itit.itf.okhttp *. ; Import io.itit.itf.okhttp.util.FileUtil; Import java.io.File; Import a java.io.InputStream; public class WopiTest { / * * / public static void main (String [] args) throws Exception { // . 1, test download file testDownload (); // 2, acquiring file information testGetInfo (); // . 3, the test information to modify the file testSave (); } / ** * function: test download file * / static void testDownload() throws Exception { String url = "http://127.0.0.1/baseService/wopi/files/123.docx/contents"; String savePath="c:\\123.docx"; InputStream is=FastHttpClient.get().url(url).build().execute().byteStream(); FileUtil.saveContent(is, new File(savePath)); } /** * 功能:测试获取文件信息 */ static void testGetInfo() throws Exception { String url = "http://127.0.0.1/baseService/wopi/files/123.docx"; String resp=. FastHttpClient.get () url (url) .build () the Execute () String ();.. System.out.println (RESP); } / ** * Function: Test editing features * Author: Yellow Sea * Time: 2019 -08-17 * * @return * @throws Exception * / static void testSave () throws Exception { String URL = "http://127.0.0.1/baseService/wopi/files/1234.docx/contents" ; String fileName = "project notice .docx" ; String filePath = "D: \\ daily work \\" + fileName; byte [] = fileContentFileUtil.getBytes(filePath); Response response = FastHttpClient.post(). url(url). addFile("content", fileName, fileContent). build(). execute(); System.out.println(response.body().string()); } }