office web apps 整合Java web項目


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM