ps:
1、ntko需要使用官方生成的授权工具包,然后按下面的集成开发方案处理;
2、客户端需要安装插件,并且插件调用的是ie浏览器,修改代码调试时,需要同时清理ie浏览器缓存;

表单设计页面

表单设计页面增加设置ntko在线编辑权限字段
image.png
修改代码:
SheetControls.js
image.png

  1. SheetAttachment: {
  2. DesignProperties: [
  3. { Name: "DataField", Text: "数据项名称", Description: "获取或设置控件绑定的数据项名称", DefaultValue: "", ValueRange: null },
  4. { Name: "AllowBatchDownload", Text: "批量下载", Description: "获取或设置是否允许批量下载", DefaultValue: true, ValueRange: [true, false] },
  5. { Name: "FileExtensions", Text: "支持扩展名", Description: "获取或设置附件上传的文件名格式,示例:.jpg,.gif", DefaultValue: "", ValueRange: null },
  6. { Name: "MaxUploadSize", Text: "最大上传大小", Description: "获取或设置限制文件上传的大小(MB),默认为 10MB", DefaultValue: 10, ValueRange: null },
  7. { Name: "IsNtko", Text: "是否支持ntko在线编辑", Description: "设置ntko在线编辑", DefaultValue: false , ValueRange: [true, false] }
  8. ]
  9. },

SheetAttachment.js
image.png

  1. //设置ntko在线编辑
  2. this.IsNtko = element.getAttribute("data-IsNtko");

附件上传前端修改

效果图
image.png
1、修改SheetAttachment.js,
第1处改动:
image.png

  1. ntkoInit(url,actionTd,this);

第2处改动:
image.png

  1. var actionTd= $("td[data-action='" + fileid + "']");
  2. actionTd.prepend($("<a href='" + resultObj.Url + "' class='fa fa-download' target='_blank' UC=true>" + SheetLanguages.Current.Download + "</a>"));
  3. ntkoInit(resultObj.Url,actionTd,this.context,true);

2、在formSheet.html的body里面加入下面代码,注意getAttachmentPath是获取文件路径的接口

  1. <script src="/Portal/kunyue/ntko/officecontrol/ntkobackground.min.js"></script>
  2. <script type="text/javascript" charset="utf-8">
  3. function ntkoInit(url,actionTd,that,isPre){
  4. //是否启用 ntko 插件
  5. if ( that.IsNtko ) {
  6. var fileurl = url;
  7. var urlParam = new Object();
  8. var i = fileurl.indexOf('?');
  9. if (i != -1) {
  10. var querystr = fileurl.substr(i + 1);
  11. var arr1 = querystr.split('&');
  12. for (i in arr1) {
  13. if(typeof(arr1[i])=='string'){
  14. var ta = arr1[i].split('=');
  15. urlParam[ta[0]] = ta[1];
  16. }
  17. }
  18. //在线编辑
  19. var bizObjectID = urlParam["BizObjectID"];
  20. var schemaCode = urlParam["BizObjectSchemaCode"];
  21. // if(!bizObjectID){
  22. // resultObj.Url = resultObj.Url.replace("&BizObjectID=", "&BizObjectID=" + $.MvcSheetUI.SheetInfo.BizObjectID);
  23. // console.log(resultObj.Url);
  24. // }
  25. var attachmentId = url.substr(url.lastIndexOf('&') - 36, 36);
  26. var Parms = "&pageId=" + attachmentId + "&BizObjectID=" + bizObjectID + "&BizObjectSchemaCode=" + schemaCode + "&DataField=" + that.DataField + "&UserID=" + $.MvcSheetUI.SheetInfo.UserID + "&WorkFlowCode=" + $.MvcSheetUI.SheetInfo.WorkflowCode + "&SequenceNo=" + $.MvcSheetUI.GetControlValue("SequenceNo") + "&UserName=" + $.MvcSheetUI.SheetInfo.UserName;
  27. console.log(Parms);
  28. var $arr = [];
  29. //添加在线预览
  30. var clickStr = "getAttachmentPath(1,\"" + Parms + "\",\"" + attachmentId + "\");return false;";
  31. $arr.push($("<a class='fa fa-eye' onclick='" + clickStr + "' >" + "在线预览" + "</a>"));
  32. //添加在线编辑
  33. if (($.MvcSheetUI.SheetInfo.SheetMode == $.MvcSheetUI.SheetMode.Work || $.MvcSheetUI.SheetInfo.SheetMode == $.MvcSheetUI.SheetMode.Originate) && that.Editable ) {
  34. clickStr = "getAttachmentPath(2,\"" + Parms + "\",\"" + attachmentId + "\");return false;";
  35. $arr.push($("<a class='fa fa-edit' onclick='" + clickStr + "' >" + "在线编辑" + "</a>"));
  36. }
  37. if(isPre){
  38. $arr = $arr.reverse();
  39. }
  40. $arr.forEach(ac => appendNtko(actionTd,ac,isPre) );
  41. }
  42. }
  43. }
  44. function appendNtko(actionTd,a,isPre){
  45. if(isPre){
  46. actionTd.prepend("&nbsp;&nbsp;");
  47. actionTd.prepend(a);
  48. }else{
  49. actionTd.append(a);
  50. actionTd.append("&nbsp;&nbsp;");
  51. }
  52. }
  53. function getQueryString(urlParam, name) {
  54. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  55. var r = urlParam.substr(1).match(reg);
  56. if (r != null) return unescape(r[2]);
  57. return null;
  58. }
  59. function getAttachmentPath(cmd, parms, attachmentId) {
  60. $.ajax({
  61. type: "get",
  62. contentType: "application/json",
  63. url: "/Portal/ntko/file/getAttachmentUrl",
  64. data:{'attachmentId': attachmentId,'BizObjectSchemaCode':getQueryString(parms, "BizObjectSchemaCode"),'BizObjectID':getQueryString(parms, "BizObjectID")},
  65. async: false,
  66. dataType: "json",
  67. success: function (result) {
  68. if(result.success){
  69. var attachmentPath = result.url;
  70. parms = parms + "&fileName=" + result.filename;
  71. if (cmd == 2) {
  72. SaveEditHtml(cmd, parms, attachmentPath);
  73. }
  74. else {
  75. openEditHtml(cmd, parms, attachmentPath);
  76. }
  77. }
  78. }
  79. });
  80. }
  81. function openEditHtml(cmd, Parms, urlPath) {
  82. //判断插件是否安装
  83. var ntkoed = ntkoBrowser.ExtensionInstalled();
  84. var domain = window.location.host;
  85. if (ntkoed) {
  86. ntkoBrowser.openWindow("http://" + domain + "/Portal/kunyue/ntko/editindex.html?cmd=" + cmd + "&urlPath=" + urlPath + Parms);
  87. } else {
  88. var iTop = ntkoBrowser.NtkoiTop(); //获得窗口的垂直位置;
  89. var iLeft = ntkoBrowser.NtkoiLeft(); //获得窗口的水平位置;
  90. window.open("http://" + domain + "/Portal/kunyue/ntko/editindex.html?cmd=" + cmd + "&urlPath=" + urlPath + Parms, "", "height=200px,width=500px,top=" + iTop + "px,left=" + iLeft + "px,titlebar=no,toolbar=no,menubar=no,scrollbars=auto,resizeable=no,location=no,status=no");
  91. }
  92. }
  93. function SaveEditHtml(cmd, Parms, urlPath) {
  94. //判断插件是否安装
  95. var ntkoed = ntkoBrowser.ExtensionInstalled();
  96. var domain = window.location.host;
  97. if (ntkoed) {
  98. ntkoBrowser.openWindow("http://" + domain + "/Portal/kunyue/ntko/editindex.html?cmd=" + cmd + "&urlPath=" + urlPath + Parms);
  99. } else {
  100. var iTop = ntkoBrowser.NtkoiTop(); //获得窗口的垂直位置;
  101. var iLeft = ntkoBrowser.NtkoiLeft(); //获得窗口的水平位置;
  102. window.open("http://" + domain + "/Portal/kunyue/ntko/editindex.html?cmd=" + cmd + "&urlPath=" + urlPath + Parms, "", "height=800px,width=1000px,top=" + iTop + "px,left=" + iLeft + "px,titlebar=no,toolbar=no,menubar=no,scrollbars=auto,resizeable=no,location=no,status=no");
  103. }
  104. }
  105. </script>

3、修改ntko工具包里面的editindex.html文件,注意保存和删除文件的接口地址

  1. <html>
  2. <head>
  3. <meta content="IE=10" http-equiv="X-UA-Compatible" />
  4. <meta http-equiv="content-type" content="text/html;charset=utf-8" />
  5. <title>首页 - OFFICE文档控件演示示例</title>
  6. <link href="officecontrol/ntkoStyle.css" rel="stylesheet" type="text/css" />
  7. <script type="text/javascript" charset="utf-8" src="/Portal/WFRes/_Scripts/jquery/jquery.js"></script>
  8. <script type="text/javascript" src="officecontrol/ntko.js"></script>
  9. <script type="text/javascript">
  10. var cmd; //命令类型
  11. function getQueryString(name) {
  12. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  13. var r = window.location.search.substr(1).match(reg);
  14. if (r != null) return unescape(r[2]);
  15. return null;
  16. }
  17. //附件在线编辑,保存后重新加载
  18. function saveDoc() {
  19. var ntko = document.getElementById("TANGER_OCX");//获得控件对象
  20. var AttachmentID = getQueryString('pageId');
  21. var BizObjectID = getQueryString('BizObjectID');
  22. var BizObjectSchemaCode = getQueryString('BizObjectSchemaCode');
  23. var DataField = getQueryString('DataField');
  24. var fileName = getQueryString('fileName');
  25. var UserID = getQueryString('UserID');
  26. var WorkFlowCode = getQueryString('WorkFlowCode');
  27. //传递参数
  28. var CPARA = "bizObjectSchemaCode="+ BizObjectSchemaCode +"&bizObjectId="+ BizObjectID +"&attachmentID=" + AttachmentID;
  29. //保存接口
  30. var domain = window.location.host;
  31. var FilePath = "http://" + domain + "/Portal/ntko/file/updateFile?attachmentID=" + AttachmentID + "&bizObjectId=" + BizObjectID + "&bizObjectSchemaCode=" + BizObjectSchemaCode;
  32. // ntko.SaveToURL("http://" + domain + "/Portal/kunyue/ntko/SaveToServerDir.jsp", "fileToUpload", CPARA, "", "", false);
  33. ntko.SaveToURL(FilePath, "fileToUpload", "", "", "", false);
  34. ntko.TrackRevisions(true);//保留痕迹
  35. alert("保存成功!");
  36. if (window.opener) { window.opener.location.href = window.opener.location.href; } window.opener = null; window.open('', '_self'); window.close();
  37. }
  38. function deleteFile(fileName) {
  39. $.ajax({
  40. type: "get",
  41. contentType: "application/json",
  42. url: "/Portal/ntko/file/deleteFile",
  43. data: {'fileName': fileName},
  44. async: false,
  45. dataType: "json",
  46. success: function (result) {
  47. }
  48. });
  49. }
  50. </script>
  51. </head>
  52. <body onload=" AttachmentID = getQueryString('pageId'); SequenceNo = getQueryString('SequenceNo'); fileName = getQueryString('fileName'); urlPath = getQueryString('urlPath'); cmd = getQueryString('cmd'); init(AttachmentID, cmd, urlPath, SequenceNo, fileName);" >
  53. <ul class="nav navbar-nav SheetToolBar" id="divTopBars" style="display:block; height:50px; width:auto; padding-top:25px;">
  54. <li class="button" style="float:right; margin-right:150px">
  55. <a href="#" onclick="javascript: deleteFile(fileName); if (window.opener) { window.opener.location.href = window.opener.location.href; } window.opener = null; window.open('', '_self'); window.close();"><i class="panel-title-icon fa fa-times toolImage"></i><span class="toolText">返回流程</span></a>
  56. </li>
  57. <li id="libtnSave" class="button" style="float:right; margin-right:100px;"><a href="#" onclick="saveDoc();"><i class="panel-title-icon fa fa-save toolImage"></i><span class="toolText">保存</span></a>
  58. </li>
  59. </ul>
  60. <div id="DataToChild" class="divBody" style="color:red;text-align:left; ">
  61. </div>
  62. <div class="divBody">
  63. <script type="text/javascript" src="officecontrol/ntkoofficecontrol.min.js"></script>
  64. </div>
  65. </body>
  66. </html>

4、ntkoofficecontrol.min.js文件需要用ntko提供的工具包里面的,这里只是增加了下载链接,需要根据实际插件的位置,替换地址:

  1. if (browser=="IE"){
  2. if(window.navigator.platform=="Win32"){
  3. document.write('<!-- 用来产生编辑状态的ActiveX控件的JS脚本--> ');
  4. document.write('<!-- 因为微软的ActiveX新机制,需要一个外部引入的js--> ');
  5. document.write('<object id="TANGER_OCX" classid="clsid:'+classid+'"');
  6. document.write('codebase="'+codebase+'" width="'+width+'" height="'+height+'"> ');
  7. document.write('<param name="MakerCaption" value="'+MakerCaption+'"> ');
  8. document.write('<param name="MakerKey" value="'+MakerKey+'"> ');
  9. document.write('<param name="ProductCaption" value="'+ProductCaption+'"> ');
  10. document.write('<param name="ProductKey" value="'+ProductKey+'"> ');
  11. document.write('<param name="NoExpireKey" value="'+NoExpireKey+'"> ');
  12. document.write('<param name="IsUseUTF8URL" value="-1"> ');
  13. document.write('<param name="IsUseUTF8Data" value="-1"> ');
  14. document.write('<param name="Caption" value="NTKO OFFICE文档控件示例演示 http://www.ntko.com"> ');
  15. document.write('<SPAN STYLE="color:red">尚未安装NTKO浏览器插件,<a href="/Portal/kunyue/ntko/officecontrol/NTKO控件安装程序-昆岳互联环境技术(江苏)有限公司.exe">下载插件</a></SPAN> ');
  16. document.write('</object> ');
  17. }
  18. if(window.navigator.platform=="Win64"){
  19. document.write('<!-- 用来产生编辑状态的ActiveX控件的JS脚本--> ');
  20. document.write('<!-- 因为微软的ActiveX新机制,需要一个外部引入的js--> ');
  21. document.write('<object id="TANGER_OCX" classid="clsid:'+classidx64+'"');
  22. document.write('codebase="'+codebase64+'" width="'+width+'" height="'+height+'"> ');
  23. document.write('<param name="MakerCaption" value="'+MakerCaption+'"> ');
  24. document.write('<param name="MakerKey" value="'+MakerKey+'"> ');
  25. document.write('<param name="ProductCaption" value="'+ProductCaption+'"> ');
  26. document.write('<param name="ProductKey" value="'+ProductKey+'"> ');
  27. document.write('<param name="NoExpireKey" value="'+NoExpireKey+'"> ');
  28. document.write('<param name="IsUseUTF8URL" value="-1"> ');
  29. document.write('<param name="IsUseUTF8Data" value="-1"> ');
  30. document.write('<param name="Caption" value="NTKO OFFICE文档控件示例演示 http://www.ntko.com"> ');
  31. document.write('<SPAN STYLE="color:red">尚未安装NTKO浏览器插件,<a href="/Portal/kunyue/ntko/officecontrol/NTKO控件安装程序-昆岳互联环境技术(江苏)有限公司.exe">下载插件</a></SPAN> ');
  32. document.write('</object> ');
  33. }
  34. }
  35. else if (browser=="firefox"){
  36. document.write('<object id="TANGER_OCX" type="application/ntko-plug" codebase="'+codebase+'" width="'+width+'" height="'+height+'" ForOnSaveToURL="ntkosavetourl" ForOndocumentopened="ntkoondocumentopened"');
  37. document.write('ForOnpublishAshtmltourl="ntkopublishashtml"');
  38. document.write('ForOnpublishAspdftourl="ntkopublishaspdf"');
  39. document.write('ForOnSaveAsOtherFormatToUrl="ntkosaveasotherurl"');
  40. document.write('_MakerCaption="'+MakerCaption+'" ');
  41. document.write('_MakerKey="'+MakerKey+'" ');
  42. document.write('_ProductCaption="'+ProductCaption+'" ');
  43. document.write('_ProductKey="'+ProductKey+'" ');
  44. document.write('_NoExpireKey="'+NoExpireKey+'" ');
  45. document.write('clsid="{'+classid+'}" >');
  46. document.write('<SPAN STYLE="color:red">尚未安装NTKO Web FireFox跨浏览器插件,<a href="/Portal/kunyue/ntko/officecontrol/NTKO控件安装程序-昆岳互联环境技术(江苏)有限公司.exe">下载插件</a></SPAN> ');
  47. document.write('</object> ');
  48. }else if(browser=="chrome"){
  49. document.write('<object id="TANGER_OCX" clsid="{'+classid+'}" ForOnSaveToURL="ntkosavetourl" ForOndocumentopened="ntkoondocumentopened"');
  50. document.write('ForOnpublishAshtmltourl="ntkopublishashtml"');
  51. document.write('ForOnpublishAspdftourl="ntkopublishaspdf"');
  52. document.write('ForOnSaveAsOtherFormatToUrl="ntkosaveasotherurl"');
  53. document.write('_IsUseUTF8URL="-1" ');
  54. document.write('_IsUseUTF8Data="-1" ');
  55. document.write('_MakerCaption="'+MakerCaption+'" ');
  56. document.write('_MakerKey="'+MakerKey+'" ');
  57. document.write('_ProductCaption="'+ProductCaption+'" ');
  58. document.write('_NoExpireKey="'+NoExpireKey+'" ');
  59. document.write('_ProductKey="'+ProductKey+'" ');
  60. document.write('codebase="'+codebase+'" width="'+width+'" height="'+height+'" type="application/ntko-plug" ');
  61. document.write('<SPAN STYLE="color:red">尚未安装NTKO Web Chrome跨浏览器插件,<a href="/Portal/kunyue/ntko/officecontrol/NTKO控件安装程序-昆岳互联环境技术(江苏)有限公司.exe">下载插件</a></SPAN> ');
  62. document.write('</object>');
  63. }else if (Sys.opera){
  64. alert("sorry,ntko web印章暂时不支持opera!");
  65. }else if (Sys.safari){
  66. alert("sorry,ntko web印章暂时不支持safari!");
  67. }
  68. window.onunload =function(){
  69. var ntkojb= ntkoBrowser.NtkoJudgingBrowsers();
  70. if(ntkojb){
  71. window.opener.ntkoCloseEvent();
  72. }
  73. }

5、增加ntko.js,和ntkoofficecontrol.min.js同级目录

  1. var ntko;//控件对象
  2. //初始化去打开文档
  3. function init(AttachmentID, cmd, urlPath, SequenceNo, fileName) {
  4. //var urlPath = getAttachmentPath(attachmentId);
  5. OFFICE_CONTROL_OBJ = document.getElementById("TANGER_OCX");
  6. ntko = document.getElementById("TANGER_OCX");
  7. if (window.navigator.platform == "Win64") {
  8. //alert("平台版文档控件,请使用32位浏览器如IE");
  9. //TANGER_OCX_OBJ.AddDocTypePlugin(".tif","tif.NtkoDocument","4.0.1.0","officecontrol/ntkooledocallx64.cab",51,true);
  10. ntko.AddDocTypePlugin(".pdf", "PDF.NtkoDocument", "4.0.2.0", "officecontrol/ntkooledocallx64.cab", 51, true);
  11. } else {
  12. ntko.AddDocTypePlugin(".pdf", "PDF.NtkoDocument", "4.0.2.0", "officecontrol/ntkooledocall.cab", 51, true);//版增加对于PDF文件的支持
  13. //TANGER_OCX_OBJ.AddDocTypePlugin(".tif","tif.NtkoDocument","4.0.1.0","officecontrol/ntkooledocallx64.cab",51,true);
  14. }
  15. //在线预览
  16. if (cmd == 1) {
  17. $("#libtnSave").hide();
  18. ntko.OpenFromURL(urlPath, false);
  19. ntko.FileSave = false;
  20. ntko.FileNew = false;
  21. ntko.FileOpen = false;
  22. ntko.FileClose = false;
  23. ntko.FilePrintPreview = false;
  24. ntko.FileSaveAs = false;
  25. ntko.FilePrint = false;
  26. ntko.SetReadOnly(true, "Abcdef",1,3);
  27. }
  28. //编辑文件
  29. else if (cmd == 2) {
  30. ntko.FileSave = true;
  31. ntko.FileNew = false;
  32. ntko.FileOpen = false;
  33. ntko.FileClose = false;
  34. ntko.OpenFromURL(urlPath, false);
  35. ntko.TrackRevisions(true);//保留痕迹
  36. ntko.EnterRevisionMode(true);
  37. ntko.FilePrintPreview = false;
  38. ntko.FilePrint = false;
  39. //生成二维码
  40. // var flag = $.trim(IsExistRWM(AttachmentID));
  41. // var pageCount = ntko.ActiveDocument.Application.Selection.Information(4);
  42. // if (flag == "") {
  43. // for (var i = 1; i <= pageCount; i++) {
  44. // ntko.ActiveDocument.Application.Selection.PageSetup.DifferentFirstPageHeaderFooter = true;
  45. // ntko.ActiveDocument.ActiveWindow.ActivePane.View.SeekView = 9;
  46. // ntko.ActiveDocument.Application.Selection.Goto(1, 1, i);;//定位到第二页
  47. // ntko.ActiveDocument.Application.Selection.ParagraphFormat.Alignment = 2;//靠右显示;
  48. // add2DGBcode(SequenceNo);
  49. // ntko.ActiveDocument.ActiveWindow.ActivePane.View.SeekView = 0;
  50. // }
  51. // }
  52. // ntko.ActiveDocument.Application.Selection.Goto(1, 1, 1);;//定位到首页
  53. }
  54. //打印
  55. else if (cmd == 3) {
  56. $("#libtnSave").hide();
  57. ntko.OpenFromURL(urlPath, false);
  58. ntko.FileSave = false;
  59. ntko.FileNew = false;
  60. ntko.FileOpen = false;
  61. ntko.FileClose = false;
  62. ntko.FilePrintPreview = true;
  63. ntko.FilePrint = true;
  64. ntko.FileSaveAs = false;
  65. ntko.SetReadOnly(true, "Abcdef", 1, 3);//Abcdef为附件修改密码
  66. }
  67. deleteFile(fileName);
  68. }

后台接口

1、增加NtkoController.java,实现3个接口:
获取附件访问地址接口:getAttachmentUrl
删除临时存储的附件:deleteFile
附件更新接口:updateFile

  1. package com.h3bpm.controller;
  2. import OThinker.Common.Organization.Models.User;
  3. import OThinker.Common.charset.H3Charset;
  4. import OThinker.H3.Controller.ControllerBase;
  5. import OThinker.H3.Entity.Data.Attachment.Attachment;
  6. import OThinker.H3.Entity.IEngine;
  7. import OThinker.H3.Entity.Tracking.UserLog;
  8. import OThinker.H3.Entity.Tracking.UserLogType;
  9. import com.h3bpm.base.user.UserValidator;
  10. import com.h3bpm.base.util.CharsetUtil;
  11. import com.h3bpm.base.util.SheetUtility;
  12. import net.sf.json.JSONObject;
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;
  15. import org.springframework.stereotype.Controller;
  16. import org.springframework.web.bind.annotation.RequestMapping;
  17. import org.springframework.web.bind.annotation.RequestMethod;
  18. import org.springframework.web.bind.annotation.RequestParam;
  19. import org.springframework.web.bind.annotation.ResponseBody;
  20. import org.springframework.web.multipart.MultipartFile;
  21. import java.io.*;
  22. /**
  23. * @author zhengyj
  24. * @date 2019/10/11
  25. * @description NTKO有关文件类控制器
  26. */
  27. @Controller
  28. @RequestMapping("/Portal/ntko/file")
  29. public class NtkoController extends ControllerBase {
  30. private static final Logger LOGGER = LoggerFactory.getLogger(ControllerBase.class);
  31. public final String folderPath = "/Portal/TempImages/storage/";
  32. @Override
  33. public String getFunctionCode() {
  34. return null;
  35. }
  36. @RequestMapping(value = "/getAttachmentUrl",method={RequestMethod.GET,RequestMethod.POST})
  37. @ResponseBody
  38. public final Object Read(String BizObjectSchemaCode, String BizObjectID, String attachmentId) throws Exception {
  39. System.out.println("..........Read1");
  40. UserLog log = null;
  41. Attachment attachment = this.getEngine().getBizObjectManager().GetAttachment(this.getUserValidator().getUserID(), BizObjectSchemaCode, BizObjectID, attachmentId);
  42. log = new UserLog(UserLogType.OpenAttachment, this.getUserValidator().getUserID(), attachment.getBizObjectSchemaCode(), attachment.getBizObjectId(), attachment.getBizObjectId(), "", attachment.getFileName(), null, SheetUtility.GetClientIP(this.request), SheetUtility.GetClientPlatform(this.request), SheetUtility.GetClientBrowser(this.request));
  43. this.getEngine().getUserLogWriter().Write(log);
  44. String fileName = System.currentTimeMillis() + attachment.getFileName();
  45. System.out.println("....read...write...fileName:" + fileName);
  46. //update by luwei : app
  47. String realPath = request.getSession().getServletContext().getRealPath("/");
  48. // 20190508 add by ousf 判断临时文件夹是否存在,不存在则创建 ------
  49. String dirPath = realPath + folderPath;
  50. File dir = new File(dirPath);
  51. if(!dir.exists()&&!dir.isDirectory()){
  52. System.out.println("....read...write...create directory:" + dir);
  53. dir.mkdirs();
  54. }
  55. String savePath = dirPath + fileName;
  56. String viewUrl = folderPath + fileName;
  57. File f = new File(savePath);
  58. FileOutputStream fs = new FileOutputStream(f);
  59. fs.write(attachment.getContent());
  60. fs.close();
  61. JSONObject jsonObject = new JSONObject();
  62. jsonObject.put("url", viewUrl);
  63. jsonObject.put("filename", fileName);
  64. jsonObject.put("extension", fileName.substring(fileName.lastIndexOf(".") + 1));
  65. jsonObject.put("success", true);
  66. return jsonObject;
  67. }
  68. @RequestMapping(value = "/deleteFile",method={RequestMethod.GET,RequestMethod.POST})
  69. @ResponseBody
  70. public final String deleteFile(String fileName) {
  71. String realPath = request.getSession().getServletContext().getRealPath("/");
  72. //删除临时文件
  73. String dirPath = realPath + folderPath;
  74. File file = new File(dirPath + fileName);
  75. if(file != null){
  76. file.delete();
  77. }
  78. return "success";
  79. }
  80. @RequestMapping(value = "/updateFile", method = RequestMethod.POST)
  81. @ResponseBody
  82. public String updateFile(String bizObjectSchemaCode, String bizObjectId, String attachmentID,@RequestParam("fileToUpload") MultipartFile fileToUpload) throws Exception {
  83. LOGGER.info(">>>开始保存文档,{},{},{}", bizObjectSchemaCode, bizObjectId, attachmentID);
  84. ByteArrayOutputStream baout = new ByteArrayOutputStream();
  85. byte[] b_array = new byte[1024];
  86. InputStream stream = fileToUpload.getInputStream();
  87. String charset = CharsetUtil.getCharset(stream);
  88. int i=0;
  89. while((i=stream.read(b_array, 0, 1024))>0){
  90. baout.write(b_array, 0, i);
  91. }
  92. byte[] contents = baout.toByteArray();
  93. if (fileToUpload.getOriginalFilename().endsWith(".txt")) {
  94. contents = new String(contents, charset).getBytes(H3Charset.DEFAULT_CHARSET);
  95. }
  96. //文件流
  97. if(contents != null){
  98. IEngine engine = this.getEngine();
  99. User user = engine.getOrganization().GetUserByCode("Administrator");
  100. Attachment attachment = engine.getBizObjectManager().GetAttachment(user.getObjectID(), bizObjectSchemaCode, bizObjectId, attachmentID);
  101. if(attachment != null){
  102. attachment.setContent(contents);
  103. UserValidator uv = this.getUserValidator();
  104. String modifiedBy = uv == null ? user.getObjectID() : uv.getUserID();
  105. //更新文件
  106. engine.getBizObjectManager().UpdateAttachment(bizObjectSchemaCode, bizObjectId, attachmentID, modifiedBy, attachment.getFileName(), attachment.getContentType(), contents, attachment.getFileFlag());
  107. LOGGER.info(">>>完成保存文档");
  108. }
  109. }
  110. return "文档保存成功";
  111. }
  112. public byte[] toByteArray(InputStream input) throws IOException {
  113. ByteArrayOutputStream output = new ByteArrayOutputStream();
  114. byte[] buffer = new byte[1024*4];
  115. int n = 0;
  116. while (-1 != (n = input.read(buffer))) {
  117. output.write(buffer, 0, n);
  118. }
  119. return output.toByteArray();
  120. }
  121. private String GetFileName(String filepath) {
  122. String filename = null;
  123. if (filepath != "") {
  124. String[] names = filepath.split("\\\\");
  125. filename = names[names.length - 1];
  126. }
  127. return filename;
  128. }
  129. }

ntko集成源码.zip