进击 office web apps 整合Java web项目

createh52个月前 (02-01)技术教程13

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

相关文章

Javaweb在线商城项目(java商城项目介绍)

《Jsp Servlet Mysql实现的Java Web在线商城项目》该项目采用的技术实现如下:jsp+servlet+jdbc+Mysql数据库+Tomcat服务器本系统实现了管理员管理用户、商品...

IDEA 新建 JavaWeb 项目(:找不到 Web Application 解决方法)

由于 IDEA 版本更新,JavaWeb 项目的创建流程也会发生一些变化,这篇文件记录了新建一个 JavaWeb 项目的全流程,其中包括一些需要注意的细节以及可能发生问题的解决方法,希望能帮到大家。I...

Jsp Servlet Mysql实现的Java Web在线商城项目源码附运行视频

今天给大家演示一款由jsp servlet MySQL实现的在线商城系统,本系统实现了管理员管理用户、商品(商品分类)、订单、留言、新闻等功能,系统项目源代码在【猿来入此】获取!前台会员注册登录,查看...

腾讯T4专家用1200个实例讲明白了Java Web项目实战,学完月入30K

内容简介《Java Web开发实战1200例》包括第一卷、第二卷共计1200个例子,每卷各计600个例子。本书以开发人员在项目开发中经常遇到的问题和必须掌握的技术为中心,介绍了应用Java Web进行...

我把公司 10 年老系统改造 Maven,真香

公司有几个老古董项目,应该是 10 年前开发的了,有一个是 JSP + Servlet,有一个还用的 SSH 框架,打包用的 Ant,是有多老啊,我想在座的各位很多都没听过吧。为了持续集成、持续部署的...

javaweb、spring、springmvc和springboot有什么区别?

1、JavaWeb是以传统的Servlet,JSP为基础的Java开发框架,没有独立的容器,需要借助Web容器才能运行;Spring是一个非常优秀的Java EE框架,是一个轻量级的,基于IoC和A...