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");
%>



	
	
	
	
    
	
    
	
	
    



    


后台代码:

 

 

@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"));
	}

 

 

 

 

 

你可能感兴趣的:(文档操作,后端)