標籤:ice package str tostring size boolean files print images
娃娃
Java代碼
- 最近拿到一個老項目,用到了weboffice ,無奈網上找找收藏學習
-
- 首先需要匯入jar包iweboffice.jar
-
- 後台作業碼如下:
Java代碼
- 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());
- }
-
- }
-
- }
WebOffice控制項的使用(一)