`
nwj2010
  • 浏览: 89630 次
  • 性别: Icon_minigender_1
  • 来自: 宁波
社区版块
存档分类
最新评论

WebOffice控件的使用(一)

阅读更多

最近拿到一个老项目,用到了weboffice ,无奈网上找找收藏学习

首先需要导入jar包iweboffice.jar

后台操作代码如下:
package weboffice;

import java.io.IOException;

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

public class WebOfficeServeltAction extends HttpServlet {
private DBstep.iMsgServer2000 MsgObj = new DBstep.iMsgServer2000(); // 创建服务对象

public WebOfficeServeltAction() {
   super();
}


public void destroy() {
   super.destroy(); 
  
}


public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

   this.doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
   response.setContentType("text/html");
   response.setCharacterEncoding("UTF-8");
   System.out.println("进来了");
   String mOption = "";
   String mUserName = "";
   String mRecordID = "";
   String mTemplate = "";
   byte[] mFileBody = null;
   String mFileName = "";
   String mFileType = "";
   String mDescript = "";
   int mFileSize = 0;
  
  
   if (request.getMethod().equalsIgnoreCase("POST")) {
    MsgObj.MsgVariant(ReadPackage(request));
    if (MsgObj.GetMsgByName("DBSTEP").equalsIgnoreCase("DBSTEP")){// 如果是合法的信息包
     mOption=MsgObj.GetMsgByName("OPTION") ;   //取得操作类型
     System.out.println("操作类型==="+mOption);
     if(mOption.equalsIgnoreCase("SAVEFILE")){
      mRecordID=MsgObj.GetMsgByName("RECORDID");     //取得文档编号
           mFileName=MsgObj.GetMsgByName("FILENAME");     //取得文档名称
           mFileType=MsgObj.GetMsgByName("FILETYPE");     //取得文档类型
           mFileSize=MsgObj.MsgFileSize();                //取得文档大小
           mUserName= MsgObj.GetMsgByName("USERNAME");     //取得保存用户名称           
           //此处可以解决乱码问题
           mFileBody=MsgObj.MsgFileBody();                //取得文档内容
           mFileBody=MsgObj.ToDocument(mFileBody);//进行分离操作
      MsgObj.MsgFileBody(mFileBody); //将分离后的文档数据读入组件对象     
                
           mDescript="通用版本";                           //版本说明
           MsgObj.MsgTextClear();// 清除文本信息
           //文档信息内容保存到数据库中或者文件中
           //以保存到文件为例
           boolean flag = MsgObj.MsgFileSave("F:/test/"+mFileName);       
           if (flag){//保存文档内容
               MsgObj.SetMsgByName("STATUS", "保存成功!");   //设置状态信息
               MsgObj.MsgError("");                          //清除错误信息
           }else{
               MsgObj.MsgError("保存失败!");                 //设置错误信息
             }
             MsgObj.MsgFileClear();         
     }
    
     //打开
     if(mOption.equalsIgnoreCase("LOADFILE")){
      mRecordID = MsgObj.GetMsgByName("RECORDID"); // 取得文档编号
      mFileName = MsgObj.GetMsgByName("FILENAME"); // 取得文档名称
      mFileType = MsgObj.GetMsgByName("FILETYPE"); // 取得文档类型
      MsgObj.MsgTextClear(); // 清除文本信息
      //如果对数据库操作,只要把数据库中文档的内容付给MsgObj.MsgFileBody就可以了
      //通过mRecordID找到数据库中的记录,然后将数据库中的文件流付给mFileBody
      if (MsgObj.MsgFileLoad("F:/test/"+mFileName+mFileType)){//调入文档
       //MsgObj.MsgFileBody(mFileBody);//从数据库中读取时用
       MsgObj.SetMsgByName("STATUS","打开成功!"); // 设置状态信息 
       MsgObj.MsgError(""); // 清除错误信息
      } else {
       MsgObj.MsgError("打开失败!"); //打开失败! 设置错误信息
      }
     }
     //修改
     if (mOption.equalsIgnoreCase("MODIFYFILE")){
      MsgObj.GetMsgByName("ATTACHID");//获得ID
      //通过ID取得记录及相应信息
      MsgObj.MsgTextClear(); // 清除文本信息
      if (MsgObj.MsgFileSave("F:/test/"+mFileName+mFileType)){// 保存文件此处添路径
       MsgObj.SetMsgByName("STATUS", "保存成功!"); // 设置状态信息 保存成功

       MsgObj.SetMsgByName("ATTACHID", "");//此处存取记录编号
       MsgObj.MsgError(""); // 清除错误信息
      } else {
       MsgObj.MsgError("保存失败!"); // 设置错误信息 保存失败!
      }
      MsgObj.MsgFileClear();
     }                       
    }else {
     MsgObj.MsgError("数据包错误!");//客户端发送数据包错误!
     MsgObj.MsgTextClear();
     MsgObj.MsgFileClear();
    }  
   }else {
    MsgObj.MsgError("请使用POST方法");//必须使用POST方法!
    MsgObj.MsgTextClear();
    MsgObj.MsgFileClear();
   }
   SendPackage(response);
}

public void init() throws ServletException {
   //生成对象实例:

   MsgObj=new DBstep.iMsgServer2000();


}


//取得客户端发来的数据包

private byte[] ReadPackage(HttpServletRequest request){
   byte mStream[]=null;
   int totalRead = 0;
   int readBytes = 0;
   int totalBytes = 0;
   try{
     totalBytes = request.getContentLength();
     mStream = new byte[totalBytes];
     while(totalRead < totalBytes){
       request.getInputStream();
       readBytes = request.getInputStream().read(mStream, totalRead, totalBytes - totalRead);
       totalRead += readBytes;
       continue;
     }
   }
   catch (Exception e){
     System.out.println(e.toString());
   }
   return (mStream);
}


//发送处理后的数据包

private void SendPackage(HttpServletResponse response){
   try{
     ServletOutputStream OutBinarry=response.getOutputStream() ;
     OutBinarry.write(MsgObj.MsgVariant()) ;
     OutBinarry.flush();
     OutBinarry.close();
   }
   catch(Exception e){
     System.out.println(e.toString());
   }

}

}
 
分享到:
评论
1 楼 836969871 2018-01-12  
[color=red][size=x-small][align=right]
[flash=200,200][list]
[*]
[b][i]
引用
[u]11111111111111[/u]
[/i][/b]
[/list][/flash]
[/align][/size][/color]

相关推荐

Global site tag (gtag.js) - Google Analytics