Java实现Excel、Word、PPT、PDF在线文档查看

Java实现Excel、Word、PPT、PDF在线文档查看

      目前遇见最好的office在线预览方案,就是通过微软提供的office web apps。

      使用office web apps主要包含,office web apps server和客户端程序,office server是需要根据官方提供的软件包进行安装配置。客户端就是我们访问office server服务的代码。

      搭建office server,需要两台服务器(当然可以是虚拟机),一台是域名服务器、一台是office server。域名服务器是搭建是使用,搭建完成后可以舍弃。搭建服务网上资料太多就不再详述了。

      对于我们的项目,需要实现文件的读取服务。大概需要实现文件基本信息的读取(文件名、文件大小、文件目录等等)和文件字节流的读取(真正文件的字节流信息)。这两个服务是我们需要为office server提供调用的,当然你必须保证你的应用和office server之间保证连通。

package com.lxkj.bass.query.dpm.util.wopi;

import java.io.File;
import java.io.IOException;

public class WopiEntity {
	public String BaseFileName;
	public String BreadcrumbFolderName;
	public String BreadcrumbDocName;
	public String OwnerId;
	public long Size;
	public String SHA256;
	public boolean AllowExternalMarketplace=true;
	public String Version;
	public boolean SupportsUpdate;
	public boolean UserCanWrite;
	public boolean SupportsLocks;
	public boolean SupportsCobalt;
	
	public WopiEntity(){
		
	}
	public String toJson(){
		return "{\"BaseFileName\":\"" + this.BaseFileName + "\","
				+"\"BreadcrumbFolderName\":\"" + this.BreadcrumbFolderName + "\","
				+"\"BreadcrumbDocName\":\"" + this.BreadcrumbDocName + "\","
				+"\"OwnerId\":\"" + this.OwnerId + "\","
				+"\"Size\":\"" + this.Size+ "\","
				+"\"AllowExternalMarketplace\":\"" + this.AllowExternalMarketplace + "\","
				+"\"Version\":\"" + this.Version + "\","
				+"\"SupportsUpdate\":\""+this.SupportsUpdate+"\","
				+"\"SupportsLocks\":\""+this.SupportsLocks+"\","
				+"\"SupportsCobalt\":\""+this.SupportsCobalt+"\","
				+"\"UserCanWrite\":\""+this.UserCanWrite+"\"}";
		
	}
	
	public WopiEntity(String filePath) throws IOException{
		File file =new File(filePath);
		if(file.exists()){
			this.BaseFileName=file.getName();
			this.OwnerId="admin";
			this.Size=file.length();
			this.Version=file.lastModified()+"";
			this.SupportsLocks=true;
			this.UserCanWrite=false;
			this.SupportsUpdate=true;
			this.SHA256=SHA256Util.FileSHA256(filePath);
		}
	}
	public String getBaseFileName() {
		return BaseFileName;
	}
	public void setBaseFileName(String baseFileName) {
		BaseFileName = baseFileName;
	}
	public String getBreadcrumbFolderName() {
		return BreadcrumbFolderName;
	}
	public void setBreadcrumbFolderName(String breadcrumbFolderName) {
		BreadcrumbFolderName = breadcrumbFolderName;
	}
	public String getBreadcrumbDocName() {
		return BreadcrumbDocName;
	}
	public void setBreadcrumbDocName(String breadcrumbDocName) {
		BreadcrumbDocName = breadcrumbDocName;
	}
	public String getOwnerId() {
		return OwnerId;
	}
	public void setOwnerId(String ownerId) {
		OwnerId = ownerId;
	}
	public long getSize() {
		return Size;
	}
	public void setSize(long size) {
		Size = size;
	}
	public String getSHA256() {
		return SHA256;
	}
	public void setSHA256(String sHA256) {
		SHA256 = sHA256;
	}
	public boolean isAllowExternalMarketplace() {
		return AllowExternalMarketplace;
	}
	public void setAllowExternalMarketplace(boolean allowExternalMarketplace) {
		AllowExternalMarketplace = allowExternalMarketplace;
	}
	public String getVersion() {
		return Version;
	}
	public void setVersion(String version) {
		Version = version;
	}
	public boolean isSupportsUpdate() {
		return SupportsUpdate;
	}
	public void setSupportsUpdate(boolean supportsUpdate) {
		SupportsUpdate = supportsUpdate;
	}
	public boolean isUserCanWrite() {
		return UserCanWrite;
	}
	public void setUserCanWrite(boolean userCanWrite) {
		UserCanWrite = userCanWrite;
	}
	public boolean isSupportsLocks() {
		return SupportsLocks;
	}
	public void setSupportsLocks(boolean supportsLocks) {
		SupportsLocks = supportsLocks;
	}
	public boolean isSupportsCobalt() {
		return SupportsCobalt;
	}
	public void setSupportsCobalt(boolean supportsCobalt) {
		SupportsCobalt = supportsCobalt;
	}
	
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lxkj.bass.query.getDmsData.utils.StringEx;


public class WebOfficeServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String[] urlInfo=this.getResquestName(request);
        String fileName=WopiUtil.decodeFileUrl(urlInfo[1]);
        String filePath=WopiUtil.decodeFileUrl(urlInfo[0]);
        File file = new File(filePath);
        if(!file.exists()){
            response.setStatus(500);
        }else{
            if(StringEx.isNull(fileName)){
                fileName=file.getName();
            }
            if("contents".equalsIgnoreCase(urlInfo[2])) {     // GetFile :返回文件流
                String contentType = "application/octet-stream";
                // 以流的形式下载文件。
                InputStream fis = new BufferedInputStream(new FileInputStream(file));
                byte[] buffer = new byte[fis.available()];
                fis.read(buffer);
                fis.close();
                response.reset();
                response.addHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
                response.addHeader("Content-Length", "" + file.length());
                OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
                response.setContentType(contentType);
                toClient.write(buffer);
                toClient.flush();
                toClient.close();
            } else {
                response.setStatus(200);
                response.setCharacterEncoding("UTF-8");
                response.setContentType("application/json;charset=UTF-8");
                WopiEntity entity=new WopiEntity(filePath);
                entity.setBaseFileName(fileName);
                entity.setBreadcrumbFolderName("数据管理平台");
                entity.setBreadcrumbDocName(fileName);
                response.getWriter().write(entity.toJson());
                response.getWriter().flush();
                response.getWriter().close();
            }
        }
    }
    public String[] getResquestName(HttpServletRequest request) throws UnsupportedEncodingException{
        String[] result=new String[3];
        String requestUri=request.getRequestURI();
        String subUrl=requestUri.substring(requestUri.indexOf("/wopi/") + 1);
        if(subUrl!=null){
            String[] res=subUrl.split("/");
            if(res!=null && res.length==4){
                result[0]=res[2];
                result[1]=res[3];
                result[2]="info";
            }else if(res!=null && res.length==5){
                result[0]=res[2];
                result[1]=res[3];
                result[2]="contents";
            }
        }
        return result;
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doGet(request, response);
    }
    public void init() throws ServletException {
    }
    public WebOfficeServlet() {
        super();
    }
    public void destroy() {
        super.destroy();
    }
}

 

这是我们项目中使用到的,应为时间比较匆忙,就使用了原声的服务方式,现在最好的就是springmvc来实现。微软在这里使用到WOPI协议,介绍文档好少啊,在这里只实现了文件的在线预览,没有实现文件的在线编辑,有哪位大神实现了在线编辑,请让小弟膜拜膜拜。

 

==================当然业余玩玩而已,都不是正式产品,不涉及版权问题。

 

------我在中原,我在郑州--------

你可能感兴趣的:(JAVA,Java,Excel,WORD,PPT,PDF,OFFICE,WEB,APPS,PPT,PDF)