office web apps 整合Java web项目

时间:2022-09-08 18:38:59

office web apps 整合Java web项目

之前两篇文章将服务器安装好了,项目主要的就是这么讲其整合到我们的项目中,网上大部分都是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:\\upload\\OA\\" + 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项目都集中下。

上一篇:搭建office web apps服务器 http://www.cnblogs.com/gkl2013/p/5667959.html

office web apps 整合Java web项目的更多相关文章

  1. office web apps 整合到自己项目中(wopi实现在线预览编辑)

    借助office web apps实现在线预览和在线编辑 我所有的代码都是用go语言编写,你可以直接编译后使用,不用再有其他的操作. 最近项目实在太忙,这几天才有时间,这次是重头戏,要好好琢磨一下怎么 ...

  2. 使用Java EE 在eclipse 开发动态的Web工程(Java web项目)

    1.使用Java EE 在eclipse 开发动态的Web工程(Java web项目)1)开发开发选项切换到JavaEE2)可以在Windows->show view中找到package exp ...

  3. Exchange 2013与 Office Web Apps 整合

    好久没写什么新文章了,这里有关Office Web Apps 的部署我就省略了,只是在创建web场我一般 会创建2个url, 如: New-OfficeWebAppsFarm -InternalUrl ...

  4. Java Web系列:Java Web 项目基础

    1.Java Web 模块结构 JSP文件和AXPX文件类似,路径和URL一一对应,都会被动态编译为单独class.Java Web和ASP.NET的核心是分别是Servlet和IHttpHandle ...

  5. Java开发工程师(Web方向) - 01.Java Web开发入门 - 第6章.蜂巢

    第6章--蜂巢 蜂巢简介 网站开发完,就需要测试.部署.在服务器上运行. 网易蜂巢: 采用Docker容器化技术的云计算平台 https://c.163.com 容器管理:容器可被视作为云主机的服务器 ...

  6. Java开发工程师(Web方向) - 01.Java Web开发入门 - 第4章.Maven

    第4章--Maven Maven实战 Java Web应用的部署: 手动式: 编译:javac -cp $CATALINA_HOME/lib/servlet-api.jar web-inf/class ...

  7. Java开发工程师(Web方向) - 01.Java Web开发入门 - 第3章.Tomcat

    第3章--Tomcat Tomcat安装与运行 Tomcat:目前最常用的基于java的web应用服务器 本课程中所有的Java代码最终都需要部署到Tomcat中运行 Tomcat的配置文件是XML的 ...

  8. Java开发工程师(Web方向) - 01.Java Web开发入门 - 第1章.Web应用开发概述

    第1章--Web应用开发概述 Web应用开发概述 浏览器-服务器架构(BS-architecture) browser/ App    ---- request ---->    server ...

  9. Java开发工程师(Web方向) - 01.Java Web开发入门 - 第5章.Git

    第5章--Git 版本控制简介 VCS (version control system) 版本控制系统:记录若干文件的修订记录的系统,帮助查阅/回到某个历史版本 LVCS本地 CVCS集中式(Cent ...

随机推荐

  1. 更新CocoaPods

    终端输入 : sudo gem install -n /usr/local/bin cocoapods –pre 更新了CocoaPods后,在原来的工程中执行了pod install命令后,报这样的 ...

  2. iOS之 Mac下抓包工具使用wireshark

    主要是mac上面网卡的授权 分三个步骤:    1.wireshark安装        wireshark运行需要mac上安装X11,mac 10.8的系统上默认是没有X11的.先去http://x ...

  3. HOJ 1640 Mobile Phone

    题意:有一个n*n的矩阵,op==1时,在(x,y)增加值z,op==2时,求以(x1,y1)和(x2,y2)构成的矩阵的和. 思路:二维线段树. 代码: #include<stdio.h&gt ...

  4. 【转】Android图片加载神器之Fresco-加载图片基础&lbrack;详细图解Fresco的使用&rsqb;

    Fresco简单的使用—SimpleDraweeView 百学须先立志—学前须知: 在我们平时加载图片(不管是下载还是加载本地图片…..)的时候,我们经常会遇到这样一个需求,那就是当图片正在加载时应该 ...

  5. poj 2289 网络流 and 二分查找

    #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #d ...

  6. 点击文字可以选中相应的checkbox

    <html><head><title>中国站长天空-网页特效-表单特效-点击文字选中的复选框</title><meta http-equiv=&q ...

  7. select、pselect、poll和epoll的区别

    select.pselect.poll和epoll函数是unix中具有I/O复用的函数.什么是I/O复用?为什么要有I/O复用?以及在什么场合下使用I/O复用?既然都具有I/O复用的功能,那这几个函数 ...

  8. hdu3483之二项式展开&plus;矩阵快速幂

    A Very Simple Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...

  9. EmguCV使用Stitcher类来拼接图像

    using System; using System.Windows; using System.Collections.Generic; using System.ComponentModel; u ...

  10. iOS UIDatePicker设置为中文的方法

    UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 20, 200, 30)]; datePick ...