最近项目中需要将weboffice改成永中office,然后花了几天功夫做好了适配工作,下面来讲讲如何进行适配。

weboffice应用面比较窄,Windows 下 IE适用,Linux不适用。

yozooffice有Windows版也有Linux版本,能兼容两个操作系统,使用火狐浏览器(51-52)版本可以使用。

话不多说,开始适配。

首先准备环境:

1.安装永中office

永中office用系统java 永中office使用教程_外部插件适配


2.安装火狐浏览器

永中office用系统java 永中office使用教程_服务端_02


版本为51,安装完了之后设置不自动更新

永中office用系统java 永中office使用教程_永中office用系统java_03


永中office用系统java 永中office使用教程_System_04


3.阅读文档(永中接口文档)

永中office用系统java 永中office使用教程_System_05


初始化会判断操作系统

永中office用系统java 永中office使用教程_外部插件适配_06


主要应用的是打开远程文档,保存远程文档方法,方法都封装好了,通过APPLET标签,调用外部应用去实现。

永中office用系统java 永中office使用教程_服务端_07


codebase的值也有说明,可以访问静态资源,也可以是网络资源,这的值可以是一个请求,去服务器下载资源。

永中office用系统java 永中office使用教程_永中office用系统java_08


服务端:

永中office用系统java 永中office使用教程_服务器_09


读取网络资源返回一个FileRenderer对象,初始化之后会返回一个office对象。

其中调用的方法都是yozoapplet.jar中的方法。

永中office用系统java 永中office使用教程_服务端_10


实际应用中基本上都是调用两个方法保存远程和打开远程。4.保存远程方法

永中office用系统java 永中office使用教程_System_11


两个参数,url和filename,调用saveURL方法。

public boolean saveURL(String URL, String fileName) {
 try {
 int type = false;
 System.out.println(“fileName==” + fileName);
 fileName = fileName.toLowerCase();
 byte type;
 if (fileName.endsWith(“eio”)) {
 type = 0;
 } else if (!fileName.endsWith(“doc”) && !fileName.endsWith(“xls”) && !fileName.endsWith(“ppt”)) {
 if (fileName.endsWith(“pdf”)) {
 type = 2;
 } else if (fileName.endsWith(“uof”)) {
 type = 3;
 } else if (fileName.endsWith(“rtf”)) {
 type = 4;
 } else if (fileName.endsWith(“txt”)) {
 type = 5;
 } else if (!fileName.endsWith(“docx”) && !fileName.endsWith(“xlsx”) && !fileName.endsWith(“pptx”)) {
 if (!fileName.endsWith(“uot”) && !fileName.endsWith(“uos”) && !fileName.endsWith(“uop”)) {
 if (fileName.endsWith(“ofd”)) {
 type = 8;
 } else {
 type = 7;
 }
 } else {
 type = 7;
 }
 } else {
 type = 6;
 }
 } else {
 type = 1;
 }
 return this.doSaveURL(URL, type) != null;
 } catch (Exception var4) {
 var4.printStackTrace();
 return false;
 }
 }
 private String doSaveURL(String serverURL, int type) {
 try {
 URL url = null;
 serverURL = serverURL.replace(" “, “%20”);
 if (serverURL.toLowerCase().startsWith(“http”)) {
 url = new URL(serverURL);
 } else {
 url = new URL(this.getCodeBase(), serverURL);
 }
 Object connection;
 if (this.connectionType == 0) {
 connection = (HttpURLConnection)url.openConnection();
 } else {
 connection = (HttpsURLConnection)url.openConnection();
 }
 ((HttpURLConnection)connection).setDoInput(true);
 ((HttpURLConnection)connection).setDoOutput(true);
 ((HttpURLConnection)connection).setRequestMethod(“POST”);
 ((HttpURLConnection)connection).setUseCaches(false);
 ((HttpURLConnection)connection).setRequestProperty(“Connection”, “Keep-Alive”);
 ((HttpURLConnection)connection).setRequestProperty(“Accept”, “/”);
 ((HttpURLConnection)connection).setRequestProperty(“Content-Type”, “multipart/form-data”);
 DataOutputStream dos = new DataOutputStream(((HttpURLConnection)connection).getOutputStream());
 Workbook book = this.app.getWorkbooks().getActiveWorkbook();
 byte[] content = this.app.getWorkbooks().getWorkbookAsByteArray(book, type);
 int len = 0;
 if (content != null) {
 len = content.length;
 }
 dos.write(content, 0, len);
 dos.flush();
 dos.close();
 InputStream inStream = ((HttpURLConnection)connection).getInputStream();
 int result = inStream.read();
 System.out.println(“result====” + result);
 inStream.close();
 ((HttpURLConnection)connection).disconnect();
 book.setSaved(true);
 return result == 0 ? null : “”;
 } catch (Exception var11) {
 var11.printStackTrace();
 System.out.println(“保存失败!”);
 return null;
 }
 }

jar包中的方法,模拟客户端发送serverlet请求,包含文件流。

服务端就按照正常serverlet请求接收

永中office用系统java 永中office使用教程_服务端_12


获取文件流,获取参数。

5.打开远程文档

永中office用系统java 永中office使用教程_服务端_13


发送请求,去服务器上获取文件,flg=123&sadf=12属于参数,写到一起,会当成一个String传到后台

public boolean openDocumentRemote(final String URL, final boolean readOnly) {
 try {
 (new Thread(this.threadGroup, new Runnable() {
 public void run() {
 Workbook workbook = YOZODTApplet.this.app.getWorkbooks().openWorkbook(URL);
 if (workbook != null) {
 Document doc = workbook.getDocuments().getActiveDocument();
 if (readOnly) {
 if (!doc.isProtected()) {
 doc.protect(new TextRange[0], “6TFCKaTeX parse error: Expected 'EOF', got '}' at position 42: … }̲ …RGB”);
 }
 }
 }
 })).start();
 return true;
 } catch (Exception var4) {
 var4.printStackTrace();
 System.out.println(“异常”);
 return false;
 }
 }

服务端获取

也是返回一个FileRenderer对象

永中office用系统java 永中office使用教程_System_14


public static Map<String, String> ConvertToMap(String parms) {
 Map<String, String> formValue = new HashMap<String, String>();
 if(!StringUtils.isEmpty(parms)){
 String[] dataTemp = parms.split(”&");
 for(String parmTemp : dataTemp){
 String[] StrParmTemp = parmTemp.split("=");
 if(StrParmTemp.length > 0){
 formValue.put(StrParmTemp[0], StrParmTemp[1]);
 }
 }
 }
 return formValue;
 }

可能没个系统差异,会导致适配的问题不同,有什么疑问,可以在下方留言一起讨论,交流。