onlyoffice 在线预览的java示例代码有吗?
时间: 2024-04-30 09:18:35 浏览: 195
是的,OnlyOffice官方提供了Java示例代码。您可以参考以下示例代码实现在线预览:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class OnlyOfficeOnlinePreview {
private static final String DOCUMENT_SERVER_URL = "http://documentserver/";
private static final String DOCUMENT_KEY = "document-key";
public static void main(String[] args) {
String url = DOCUMENT_SERVER_URL + "/ConvertService.ashx" + "?url=" + DOCUMENT_SERVER_URL + "/WebEditors/" + DOCUMENT_KEY + "/doc.docx" + "&outputtype=pdf";
try {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
其中,`DOCUMENT_SERVER_URL`是OnlyOffice Document Server的URL,`DOCUMENT_KEY`是文档的唯一标识符。在示例代码中,我们使用了HTTP GET请求获取PDF格式的预览文件。您可以根据需要调整代码实现其他功能。
阅读全文