永中office插件适配详解
最近項(xiàng)目中需要將weboffice改成永中office,然后花了幾天功夫做好了適配工作,下面來講講如何進(jìn)行適配。
weboffice應(yīng)用面比較窄,Windows 下 IE適用,Linux不適用。
yozooffice有Windows版也有Linux版本,能兼容兩個操作系統(tǒng),使用火狐瀏覽器(51-52)版本可以使用。
話不多說,開始適配。
首先準(zhǔn)備環(huán)境:
1.安裝永中office
2.安裝火狐瀏覽器
版本為51,安裝完了之后設(shè)置不自動更新
3.閱讀文檔(永中接口文檔)
初始化會判斷操作系統(tǒng)
主要應(yīng)用的是打開遠(yuǎn)程文檔,保存遠(yuǎn)程文檔方法,方法都封裝好了,通過APPLET標(biāo)簽,調(diào)用外部應(yīng)用去實(shí)現(xiàn)。
codebase的值也有說明,可以訪問靜態(tài)資源,也可以是網(wǎng)絡(luò)資源,這的值可以是一個請求,去服務(wù)器下載資源。
服務(wù)端:
讀取網(wǎng)絡(luò)資源返回一個FileRenderer對象,初始化之后會返回一個office對象。
其中調(diào)用的方法都是yozoapplet.jar中的方法。
實(shí)際應(yīng)用中基本上都是調(diào)用兩個方法保存遠(yuǎn)程和打開遠(yuǎn)程。
4.保存遠(yuǎn)程方法
兩個參數(shù),url和filename,調(diào)用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包中的方法,模擬客戶端發(fā)送serverlet請求,包含文件流。
服務(wù)端就按照正常serverlet請求接收
獲取文件流,獲取參數(shù)。
5.打開遠(yuǎn)程文檔
發(fā)送請求,去服務(wù)器上獲取文件,flg=123&sadf=12屬于參數(shù),寫到一起,會當(dāng)成一個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;
}
}
服務(wù)端獲取
也是返回一個FileRenderer對象
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;
}
可能沒個系統(tǒng)差異,會導(dǎo)致適配的問題不同,有什么疑問,可以在下方留言一起討論,交流。
總結(jié)
以上是生活随笔為你收集整理的永中office插件适配详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 离线状态能不能翻译?手机翻译软件离线小测
- 下一篇: GDAL的安装和配置---出现的问题