之前两篇文章将服务器安装好了,项目主要的就是这么讲其整合到我们的项目中,网上大部分都是asp.net的,很少有介绍Java如何整合的,经过百度,终于将其整合到了我的项目中。
第一建个servlet拦截器
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
String uri = httpRequest.getRequestURI; /// wopihost/wopi/files/Excel.xlsx
// 解决中文乱码问题
String fileUri = URLDecoder.decode(uri.substring(uri.indexOf("/WOPI/") + 1, uri.length), "UTF-8"); // /wopi/files/test.docx
//String filePath = request.getServletContext.getRealPath("/") + fileUri;
String filePath = "D:uploadOA" + fileUri;
if (fileUri.endsWith("/contents")) { // GetFile :返回文件流
filePath = filePath.substring(0, filePath.indexOf("/contents"));
getFile(filePath, httpResponse);
} else { // CheckFileInfo :返回json
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json;charset=UTF-8");
PrintWriter out = null;
try {
out = response.getWriter;
out.write(FileUtil.checkFileInfo(filePath));
} catch (IOException e) {
e.printStackTrace;
} finally {
if (out != null) {
out.close;
}
}
}
return;
}
private HttpServletResponse getFile(String path, HttpServletResponse response) {
try {
// path是指欲下载的文件的路径。
File file = new File(path);
// 取得文件名。
String filename = file.getName;
String contentType = "application/octet-stream";
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte buffer = new byte[fis.available()];
fis.read(buffer);
fis.close;
// 清空response
response.reset;
// 设置response的Header
response.addHeader("Content-Disposition","attachment;filename=" + new String(filename.getBytes("utf-8"), "ISO-8859-1"));
response.addHeader("Content-Length", "" + file.length);
OutputStream toClient = new BufferedOutputStream(response.getOutputStream);
response.setContentType(contentType);
toClient.write(buffer);
toClient.flush;
toClient.close;
} catch (IOException ex) {
ex.printStackTrace;
}
return response;
}
工具类FileUtil代码如下:
/**
* 获取文件基本信息
*
* @param filePath文件路径
* @return
*/
public static String checkFileInfo(String filePath) {
File file = new File(filePath);
String baseFileName = null; // 文件名
String ownerId = null; // 文件所有者的唯一编号
long size = 0; // 文件大小,以bytes为单位
// String sha256 = null; //文件的256位bit的SHA-2编码散列内容
long version = 0; // 文件版本号,文件如果被编辑,版本号也要跟着改变
if (file.exists) {
// 取得文件名。
baseFileName = file.getName;
size = file.length;
// 取得文件的后缀名。
// String ext = baseFileName.substring(baseFileName.lastIndexOf(".")
// + 1);
ownerId = "admin";
// sha256 = new SHAUtils.SHA(FileUtils.readByByte(file),
// "SHA-256");
version = file.lastModified;
}
return "{"BaseFileName":"" + baseFileName + "","OwnerId":"" + ownerId + "","Size":"" + size
+ "","AllowExternalMarketplace":"" + true + "","Version":"" + version + ""}";
}
之前安装好后测试时打开的
xml:http://docview.mingdao.com/hosting/discovery,不同格式的文档调用不同,具体可以详细看看。
访问
http://127.0.0.1:8080/xxxx/wopi/files/test.docx/contents则下载该文件访问
http://xxx.docview.com/wv/wordviewerframe.aspx?WOPISrc=
http://xxx.xxx.com/blog/http%3A%2F%2F192.168.1.11%3A8080%2Fwopihost%2Fwopi%2Ffiles%2Ftest.docx 进行预览
之前在网上查询资料都是些零散的,今天将Java web整合office web apps从部署到整合到web项目都聚焦下。