进击 office web apps 整合Java web项目

  • 时间:2025-11-11 20:33 作者: 来源: 阅读:0
  • 扫一扫,手机访问
摘要:之前两篇文章将服务器安装好了,项目主要的就是这么讲其整合到我们的项目中,网上大部分都是asp.net的,很少有介绍Java如何整合的,经过百度,终于将其整合到了我的项目中。第一建个servlet拦截器public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOE

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

  • 全部评论(0)
最新发布的资讯信息
【系统环境|】最低 2 美元,这 55 款 macOS & Windows 应用一次全都入手(2025-11-11 22:01)
【系统环境|】SCI期刊对论文图片有哪些要求?(2025-11-11 22:00)
【系统环境|】论文缩写大全,拿走不谢(2025-11-11 22:00)
【系统环境|】阿甘正传高频词整理 GRE托福四六级词汇整理(2025-11-11 21:59)
【系统环境|】矢量图形编辑应用程序-WinFIG(2025-11-11 21:59)
【系统环境|】Figma上市首日暴涨250%的深层逻辑:为什么AI时代协作平台更加不可替代?(2025-11-11 21:58)
【系统环境|】FigJam是什么?一文读懂在线白板软件的方方面面!(2025-11-11 21:58)
【系统环境|】在windows上有什么好用的书写白板软件?(2025-11-11 21:57)
【系统环境|】Docker基础应用之nginx(2025-11-11 21:57)
【系统环境|】VS Code 新手必装插件清单(2025-11-11 21:56)
手机二维码手机访问领取大礼包
返回顶部