微软OfficeOnline服务安装部署,wopi代码实现--------(5)java代。。。本⽂是第5篇
所有的软件安装和配置全部ok了,下⾯就是代码运⾏了.
1.域控服务器安装java环境
我们的java 代码在域控服务器上⾯运⾏,所以要现在域控服务器上⾯安装java环境,这个就不演⽰了,⽹上windows安装java教程⼀⼤推2.启动office Online 服务器场
这⾥⼜是⼀个新名词:服务器场 ,打开安装office Online 的主机
打开powerShell
(1) 输⼊ Import-Module OfficeWebApps 回车.
–AllowHttp 是否允许http访问;
–ExternalUrl 外部访问地址,⼀般是服务器的ip地址;
–EditingEnabled 允许编辑office。
输⼊第⼆条命令后 等待⼀会,就会ok,如下图:
这个时候⽤可以看下有没有启动起来
当然这两个地址在域控服务器上⾯访问 也是ok 的
3.安装好java环境后,启动服务场后,开始上代码
package ller;
import com.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import ity.FileInfo;
import dec.binary.Base64;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.URLDecoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* WOPI HOST
* Created by ethendev on 2017/4/15.
*/
@RestController
@RequestMapping(value="/wopi")
public class WopiHostContrller {
@Value("${file.path}")
private String filePath;
/**
* 获取⽂件流
* @param name
* @param name
* @param response
*/
@GetMapping("/files/{name}/contents")
public void getFile(@PathVariable(name = "name") String name, HttpServletResponse response) { System.out.println("GET获取⽂件啦");
InputStream fis = null;
OutputStream toClient = null;
try {
// ⽂件的路径
String path = filePath + name;
File file = new File(path);
// 取得⽂件名
String filename = Name();
// 以流的形式下载⽂件
fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
/
/ 清空response
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" +
new Bytes("utf-8"), "ISO-8859-1"));
response.addHeader("Content-Length", "" + file.length());
toClient = new OutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
System.out.println("GET获取⽂件Contents结束");
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
fis.close();
toClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/
**
* 保存更新⽂件
* @param name
* @param content
*/
@PostMapping("/files/{name}/contents")
public void postFile(@PathVariable(name = "name") String name, @RequestBody byte[] content) { System.out.println("POST获取⽂件Contents啦");
// ⽂件的路径
String path = filePath + name;
File file = new File(path);
FileOutputStream fop = null;
try {
if (!ists()) {
}
fop = new FileOutputStream(file);
fop.write(content);
fop.flush();
System.out.println("POST获取⽂件结束");
System.out.println("------------ save file ------------ ");
} catch (IOException e) {
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fop.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 获取⽂件信息
* @param request
* @param response
* @return
* @throws UnsupportedEncodingException
*/
@GetMapping("/files/{name}")
public void getFileInfo(HttpServletRequest request, HttpServletResponse response) {
System.out.println("获取⽂件啦");
String uri = RequestURI();
FileInfo info = new FileInfo();
try {
/
/ 获取⽂件名, 防⽌中⽂⽂件名乱码
String fileName = URLDecoder.decode(uri.substring(uri.indexOf("wopi/files/") + 11, uri.length()), "UTF-8");
if (fileName != null && fileName.length() > 0) {
System.out.println("⽂件不为空啊");
File file = new File(filePath + fileName);
if (ists()) {
// 取得⽂件名
info.Name());
info.setSize(file.length());
info.setOwnerId("admin");
info.setVersion(file.lastModified());
info.setSha256(getHash256(file));
info.setAllowExternalMarketplace(true);
info.setUserCanWrite(true);
info.setSupportsUpdate(true);
info.setSupportsLocks(true);
}
java怎么编写}
ObjectMapper mapper = new ObjectMapper();
String Json = mapper.writeValueAsString(info);
response.setContentType("application/json;charset=utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (JsonProcessingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取⽂件的SHA-256值
* @param file
* @return
*/
public static String getHash256(File file) {
String value = "";
// 获取hash值
try {
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论