• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >微信公众号 > 微信或手机浏览器在线显示office文件(已测试ios、android)

微信或手机浏览器在线显示office文件(已测试ios、android)

作者:匿名 字体:[增加 减小] 来源:互联网

匿名通过本文主要向大家介绍了微信 手机 浏览器 office等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
最近开发微信企业号,发现微信andriod版内置浏览器在打开文件方面有问题,但是ios版没有问题,原因是ios版使用的是safari浏览器 支持文档直接打开,但是andriod版使用的是腾讯浏览器x5内核,不知道什么原因不支持,可能是集成出现的问题,这里提供解决方法,这种方法也同样适用手机浏览器或者安卓开发。通过此方法可以在微信上开发自己的第三方应用,或者解决自己的项目问题,解决方法及核心代码如下:
1、判断浏览器类型
HttpServletRequest req = ServletActionContext.getRequest();
String userAgent=req.getHeader("User-Agent");//里面包含了设备类型
2、IOS版直接使用流输出
Andriod版利用openoffice+jod转换成html,然后对html内容重新编辑,文件中有图片的将路径改为网络路径或者采用流输出(改成网络路径注意特殊符号,如+号会变成空格)

/**
 * 从OA上抓取文件
 * author 牟云飞
 * company 海颐软件股份有限公司
 * tel  15562579597
 * qq  1147417467
 * team 客服产品中心/于洋
 * @return
 */
 public String getFileFromOa(){ 
  
 HttpServletRequest req = ServletActionContext.getRequest();
 String userAgent=req.getHeader("User-Agent");//里面包含了设备类型
 if(-1!=userAgent.indexOf("iPhone")){
 //-----------------//
 //此方法需要浏览器自己能够打开,ios可以但是微信andriod版内置浏览器不支持
 //-----------------//
 //如果是苹果手机
 //获得文件地址
 String fileUrl = ServletActionContext.getRequest().getParameter("fileUrl");
 fileUrl.replaceAll("%20", "\\+");//转换加号
 String strURL = MessageUtil.oaUrl+fileUrl;
 String fileType=strURL.substring(strURL.lastIndexOf(".")+1,strURL.length());
 //获得图片的数据流
 try {
 URL oaUrl = new URL(strURL);
 HttpURLConnection httpConn = (HttpURLConnection) oaUrl.openConnection();
 InputStream in = httpConn.getInputStream();
 //获取输出流
 HttpServletResponse response = ServletActionContext.getResponse();
 req.setCharacterEncoding("UTF-8");
 response.setCharacterEncoding("UTF-8");
 String name=fileUrl.substring(fileUrl.lastIndexOf("/")+1, fileUrl.length());
  
 response.setHeader("Content-Disposition", 
      "attachment;filename=" + 
       new String( (name ).getBytes(), 
          "iso-8859-1"));
 if("doc".equals(fileType)||"docx".equals(fileType)){
  response.setContentType("application/msword");
 }else if("xls".equals(fileType)||"xlsx".equals(fileType)){
  response.setContentType("application/msexcel"); 
 }else{
  response.setContentType("application/"+fileType);
 }
 OutputStream out = response.getOutputStream();
 //输出图片信息
 byte[] bytes = new byte[1024]; 
 int cnt=0; 
 while ((cnt=in.read(bytes,0,bytes.length)) != -1) { 
  out.write(bytes, 0, cnt); 
 } 
 out.flush();
 out.close();
 in.close();
  
 } catch (MalformedURLException e) {
 e.printStackTrace();
 } catch (IOException e) {
 e.printStackTrace();
 }
 return null;
 }else{
 //如果非苹果手机,自己处理文档
  
 //获得文件地址
 String fileUrl = ServletActionContext.getRequest().getParameter("fileUrl");
   
 fileUrl.replaceAll("%2B", "\\+");//转换加号
 String strURL = MessageUtil.oaUrl+fileUrl;
 //在本地存放OA文件,然后转换成html,再对文档中的图片路径进行修改,最后输出到页面
 try {
 URL oaUrl = new URL(strURL);
 HttpURLConnection httpConn = (HttpURLConnection) oaUrl.openConnection();
 InputStream in = httpConn.getInputStream();
 //获取输出流
 HttpServletResponse response = ServletActionContext.getResponse();
 req.setCharacterEncoding("UTF-8");
 response.setCharacterEncoding("UTF-8");
 String name=fileUrl.substring(fileUrl.lastIndexOf("/")+1, fileUrl.length());
  
 //首先判断本地是否存在
 String path=req.getRealPath("");
 path=path.substring(0, path.lastIndexOf("\\")+1);
 File htmlFile=new File(path + "OaFileToHtml\\"+name+".html");
 if(!htmlFile.exists()){
  //判断文件夹是否存在,创建文件夹
  String oaFilePath=path + "OaFile";//存放OA文档的文件夹路径;
  File oaFiles=new File(oaFilePath);
  if(!oaFiles.exists()){
  //如果文件夹不存在创建文件夹
  oaFiles.mkdirs();
  }
  //将OA消息存入本地
  File oafile=new File(oaFiles+ File.separator +name);
  OutputStream out = new FileOutputStream(oafile);
  //输出图片信息
  byte[] bytes = new byte[1024]; 
  int cnt=0; 
  while ((cnt=in.read(bytes,0,bytes.length)) != -1) { 
  out.write(bytes, 0, cnt); 
  } 
  out.flush();
  out.close();
  in.close();
  //转换成html
  String htmlFilePath =path + "OaFileToHtml";//OA文件转成html的位置
  String htmlcontext=ConvertFileToHtml.toHtmlString(oafile, htmlFilePath);
  req.setAttribute("htmlcontext", htmlcontext);
 }else{
  //已经存在转换成功的文档
  StringBuffer htmlSb = new StringBuffer();
  try {
  BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(htmlFile),Charset.forName("gb2312")));
  while (br.ready()) {
  htmlSb.append(br.readLine());
  }
  br.close();
  } catch (FileNotFoundException e) {
  e.printStackTrace();
  } catch (IOException e) {
  e.printStackTrace();
  }
  // HTML文件字符串
  String htmlStr = htmlSb.toString();
  //System.out.println("htmlStr=" + htmlStr);
  // 返回经过清洁的html文本
  req.setAttribute("htmlcontext", ConvertFileToHtml.clearFormat(htmlStr, ""));
 }
  
 } catch (MalformedURLException e) {
 e.printStackTrace();
 } catch (IOException e) {
 e.printStackTrace();
 }
 return "lookfile";
 }
  
 }

------------------将word转换成html文件,并读取内容-------------------------

package com.haiyisoft.wx.util;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
 
/**
 * * 端口启动命令:
 * soffice -headless -accept="socket,port=8100;urp;
 *
 * 
 * author 牟云飞
 * company 海颐软件股份有限公司
 * tel  15562579597
 * qq  1147417467
 * team 客服产品中心/于洋
 * 
 */
public class ConvertFileToHtml {
 /**
 * 将word文档转换成html文档
 * @param docFile 需要转换的word文档
 * @param filepath 转换之后html的存放路径
 * @return 转换之后的html文件
 */
 public static File convert(File docFile, String filepath) {
 
 // 创建保存html的文件
 String fileName=docFile.getName();
 File htmlFile = new File(filepath + "/" + fileName + ".html");
 // 创建Openoffice连接
 OpenOfficeConnection con = new SocketOpenOfficeConnection(8100);
 try {
 // 连接
 con.connect();
 } catch (ConnectException e) {
 System.out.println("获取OpenOffice连接失败...");
 e.printStackTrace();
 }
  
 // 创建转换器
 DocumentConverter converter = new OpenOfficeDocumentConverter(con);
 // 转换文档问html
 converter.convert(docFile, htmlFile);
 // 关闭openoffice连接
 con.disconnect();
 return htmlFile;
 }
 
 /**
 * 
 * 将word转换成html文件,并且获取html文件代码。
 * @param docFile 需要转换的文档
 * @param filepath 文档中图片的保存位置
 * @return 转换成功的html代码
 */
 public static String toHtmlString(File docFile, String filepath) {
 // 转换word文档
 File htmlFile = convert(docFile, filepath);
 System.out.println(htmlFile.getAbsolutePath());
 // 获取html文件流
 StringBuffer htmlSb = new StringBuffer();
 try {
 BufferedReader br = new BufferedReader(new In



 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

相关文章

  • 微信开发之推送消息的实现
  • android微信登陆、分享做了一段时间了发现的一些坑
  • C#开发微信门户及应用-微信企业号的消息和事件的接收处理及解密
  • 分享微信开发之支付功能(前端)的实现
  • C#微信公众平台开发之高级群发接口
  • 微信开发之接收文本消息的接口和参数
  • 微信小程序开发制作麦克风动画 实现放大、淡出效果
  • 微信开发入门(六)用户数据解密
  • 关于录音功能的详细介绍
  • 使用.NET微信开发PC端微信扫码注册和登录功能实现代码

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • 微信开发如何做本地调试
    • Senparc.Weixin.MP SDK 微信公众平台开发教程(三):微信公众平台开发验证
    • 微信开发系列----02:实现POST请求响应
    • 微信开发之公交换乘功能代码详解
    • C#微信开发:根据经纬度获取地址
    • 微信是如何用libco支撑8亿用户的
    • C#开发微信门户及应用微信支付接入和API封装使用
    • 微信开发之上传临时素材介绍
    • 微信公众号开发的详细介绍
    • 微信公众号支付(一)如何获取用户openId

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有