dsoframer在線編輯(讀取保存服務器文檔)

jsp代碼:

 

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/pages/common/getPath.jsp" %>
<%@ page import="com.daorigin.common.dto.SessionDto" %>
<%
SessionDto sessionDto=(SessionDto) request.getSession().getAttribute("sessionDto");
String filePath = request.getParameter("filePath");
String fileType = request.getParameter("fileType");
%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title></title>
	<link rel="stylesheet" type="text/css" href="<%=path%>/public/css/smelp.css">
    <link type="text/css" rel="stylesheet" href="<%=path%>/public/bootstrap32/css/bootstrap.min.css" />
	<link type="text/css" rel="stylesheet" href="<%=path%>/public/temp/font-awesome.min.css" />
    <link type="text/css" rel="stylesheet" href="<%=path%>/public/temp/main2.css" />
	<script type="text/javascript" src="<%=path%>/public/js/officeOnline.js"></script>
	<script type="text/javascript" src="<%=path%>/public/js/jquery-1.9.1.min.js"></script>
    <script type="text/javascript">
    $(function(){
    	var height = window.innerHeight-47;
    	$("#oframe").attr("height",height+"px");
    })
    function openWord(){
	  	var filePath = '<%=filePath%>';
    	var webUrl = "<%=basePath%>/common/file/getWord.do?filePath="+filePath;
    	OpenWebWord(webUrl);
    	ToggleMenubar();
    	ToggleRibbonBar();
    	ToggleMenubar();
    	ToggleShowRevisions(true);
    	SetUserName('<%=sessionDto.getUserName()%>');
    }
    
    function saveToWeb(){
    	var filePath = '<%=filePath%>';
    	document.getElementById("oframe").HttpInit();
    	document.getElementById('oframe').HttpAddPostString("filePath",filePath);
    	document.getElementById("oframe").HttpAddPostCurrFile("FileData",""); 
    	document.getElementById("oframe").HttpPost("<%=basePath%>/common/file/saveToWeb.do");
   		alert("保存成功!");
    }
    
    function isIE() { //ie?  
        if (!!window.ActiveXObject || "ActiveXObject" in window)  
            return true;  
        else  
            return false;  
    }
    
    function isSupportOfficeOnline(){
    	var filePath = '<%=filePath%>';
    	var fileType = '<%=fileType%>';
    	if(filePath == ''){
    		window.location.href="<%=basePath%>/contInfo/anlysisError.do?errorType=2";
    	}else if(fileType != ".docx"){
    		window.location.href="<%=basePath%>/contInfo/anlysisError.do?filePath="+filePath+"&errorType=1";
    	}else{
    		if(isIE()){
    			openWord();
    		}else{
    			window.location.href="<%=basePath%>/contInfo/anlysisError.do?filePath="+filePath+"&errorType=0";
    		}
    	}
    }
    </script>
</head>
<body οnlοad="InitEvent();isSupportOfficeOnline();" style="background-color: #ffffff;">
<input type="hidden" name="filePath" id="filePath" value="${commonFile.filePath}">
    <div>
        <table width="100%">
            <tr>
                <td>
                <input type="button" οnclick="saveToWeb()" name="保存" class="btn btn-info btn-sm" style="margin-top: 20px;float:right" value="保存">
                   <object classid="clsid:00460182-9E5E-11d5-B7C8-B8269041DD57" id="oframe" width="100%"
                            height="100%" codebase="dsoFramer.CAB#version=2,2,1,2">
                        <param name="BorderStyle" value="1" />
                        <param name="TitlebarColor" value="52479" />
                        <param name="TitlebarTextColor" value="0" />
                        <param name="Menubar" value="0" />
                        <param name="Titlebar" value="0" />
                        <param name="Menubar" value="0" />
                    </object>
                    <div style="display: none">
                        <!-- dsoframe事件 開始 -->
                        <script type="text/javascript" language="jscript" for="oframe" event="OnDocumentOpened(str,obj)">
                            OnDocumentOpened(str, obj);
                        </script>
                        <script type="text/javascript" language="jscript" for="oframe" event="OnDocumentClosed()">
                            OnDocumentClosed();
                        </script>
                        <!-- dsoframe事件 結束 -->
                    </div>
                </td>
            </tr>
        </table>
    </div>
</body>
</html>


後臺代碼:

 

 

@RequestMapping(value = "/getWord.do")
	public void getWord(String filePath,HttpServletRequest request,
			HttpServletResponse response) {
		String upload_path = ConfigManager.getConfigValue("upload_path");
		String path = upload_path + "\\" + filePath;
		File file = new File(path);
		
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Content-type", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
		response.setHeader("Content-Disposition", "attachment;filename=" + "asdas1.docx");
		response.setDateHeader("Expires", 0);

		FileInputStream fis = null;
		OutputStream os = null;
		try {
			fis = new FileInputStream(file);
			os = response.getOutputStream();

			int count = 0;
			byte[] buffer = new byte[1024 * 8];
			while ((count = fis.read(buffer)) != -1) {
				os.write(buffer, 0, count);
				os.flush();
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fis.close();
				os.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

 

@RequestMapping(value = "/saveToWeb.do")
	public void saveToWeb(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String flag = "0";
		String filePath = "";
		FileItemFactory dfif = new DiskFileItemFactory();
		ServletFileUpload servletfileupload = new ServletFileUpload(dfif);
		try{
			List fileItems = servletfileupload.parseRequest(request);  
			// 依次處理請求     
			Iterator iter = fileItems.iterator();  
			while (iter.hasNext()) {  
				FileItem item = (FileItem) iter.next();  
				if (item.isFormField()) {  
					// 如果item是正常的表單域     
					String name = item.getFieldName();  
					String value = item.getString("UTF-8");  
					if(name.equals("filePath")){
						filePath=value;//附件標題賦值  
					}  
				} else {  
					//獲取文件實體
					String upload_path = ConfigManager.getConfigValue("upload_path");
					File savefile = new File(upload_path + "\\" + filePath);
					item.write(savefile);
				}  
			} 
			flag = "0";
		}catch (Exception e) {
			flag = "1";
		}
		response.setHeader("Content-type","text/html;charset=UTF-8");//向瀏覽器發送一個響應頭,設置瀏覽器的解碼方式爲UTF-8
	    OutputStream stream = response.getOutputStream();
	    stream.write(flag.getBytes("UTF-8"));
	}

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章