📄 mysavetempattachmentaction.java
字号:
package com.mdcl.mocha.jlcmcc.document.action;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.mdcl.mocha.bpm.business.sdk.commons.event.BPMBusinessListenerException;
import com.mdcl.mocha.bpm.business.sdk.commons.exception.BPMBusinessException;
import com.mdcl.mocha.bpm.business.sdk.oaapp.docmanager.IAttachmentManager;
import com.mdcl.mocha.bpm.document.action.SaveTempAttachmentAction;
import com.mdcl.mocha.bpm.document.action.entity.DocumentInstBean;
import com.mdcl.mocha.bpm.document.action.util.SessionControl;
import com.mdcl.mocha.bpm.sdk.ServiceManager;
import com.mdcl.mocha.bpm.sdk.document.entity.IDocAttachment;
import com.mdcl.mocha.bpm.sdk.document.entity.IDocBeanFactory;
import com.mdcl.mocha.bpm.sdk.license.ExpiryException;
import com.mdcl.mocha.bpm.sdk.license.LicenseException;
import com.mdcl.mocha.bpm.sdk.license.NotFoundModuleException;
import com.mdcl.mocha.bpm.sdk.osm.IEmpInstMgmtService;
import com.mdcl.mocha.bpm.sdk.osm.entity.EmpInstBean;
import com.mdcl.mocha.bpm.webcommons.constant.ISessionAttributeConstant;
/**
* <strong>Title : mySaveTempAttachmentAction<br></strong>
* <strong>Description : </strong><br>
* <strong>Create on : 2007-10-10<br></strong>
* <p>
* <strong>Copyright (C) Mocha Software Co.,Ltd.<br></strong>
* <p>
* @author zhanghd zhanghd@mochasoft.com.cn<br>
* @version <strong>吉林移动BPM一期</strong><br>
* <br>
* <strong>修改历史:</strong><br>
* 修改人 修改日期 修改描述<br>
* -------------------------------------------<br>
* <br>
* <br>
*/
public class mySaveTempAttachmentAction extends SaveTempAttachmentAction{
/**
* 上传单个文件最大字节数 根据需求参数由配置文件传入
*/
private static final long FILE_SIZE_MAX = 4 * 1024 * 1024;
/**
* 上传附件总大小最大为20M
*/
private static final long FILE_TOTAL_SIZE_MAX = 20 * 1024 * 1024;
/**
* action方法
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// 文种实例
String t_boInsId = request.getParameter("BoInsId");
// 任务ID
String t_taskId = request.getParameter("TaskId");
// EI ID
String t_eiID = request.getParameter("EiID");
// 文种实例状态
String t_boInsState = request.getParameter("BoInsState");
// 取得accountId
// 附件上载工具
DiskFileUpload t_upload = new DiskFileUpload();
t_upload.setHeaderEncoding("UTF-8");
// 设置单个附件大小上限
t_upload.setSizeMax(FILE_SIZE_MAX);
// 获得附件上载session
DocumentInstBean t_getSession = (DocumentInstBean) SessionControl
.getAttribute(request, t_boInsId);
if(t_getSession == null){
request.getSession().setAttribute(ISessionAttributeConstant.ERR_EXCEPTION_KEY,
new Exception(getMessageValue(request, "page",
"session.timeout.error")));
return mapping.findForward("sessionTimeOut");
}
// 附件总大小是否超过20M
String t_attachTotalSizeMore = "NO";
// 附件单个大小是否超过4M
String t_sigleSizeMore = "NO";
// 请求解析的列表
List t_items = null;
// 解析请求
try {
IAttachmentManager t_attachmentManager = getServiceManagerInstance();
// 请求解析成列表
t_items = t_upload.parseRequest(request);
// 用来临时存储附件的信息
List t_tempSaveList = null;
// 开始序列
int t_beginNumber = -1;
// 从session取新增加的附件
t_tempSaveList = t_getSession.getAttachTempList();
if (t_tempSaveList == null || t_tempSaveList.size() == 0) {
t_beginNumber = 0;
t_tempSaveList = new ArrayList();
} else {
t_beginNumber = t_tempSaveList.size();
}
// 从session取出TotalSize
String t_size = t_getSession.getTotalSize();
// 已经存在的附件总大小
long t_totalSize = Long.parseLong(t_size);
IDocBeanFactory factory = (IDocBeanFactory) ServiceManager
.getInstance().getService(IDocBeanFactory.class);
IDocAttachment t_Attachment = factory.getDocAttachment();
// 循环items内容
for (int t_i = 0; t_i < t_items.size(); t_i++) {
FileItem t_item = (FileItem) t_items.get(t_i);
// 判断文件项是否为表单域
if (!t_item.isFormField()) { // 是文件
Map t_map = new HashMap();
// 单个附件大小
long t_oneattachsize = t_item.getSize();
// 取得文件名和扩展名
String t_realName = getRealName(t_item);
String t_fileName = "";
String t_fileType = "";
if (t_realName.lastIndexOf(".") != -1) {
t_fileName = t_realName;
t_fileType = t_realName.substring(t_realName.lastIndexOf(".") + 1);
} else {
t_fileName = t_realName;
}
/**集中采购环节在附件名前加部门名 start
* tagName 附件标签名
* temp 流程状态
* jlbpm zhanghd 2007-09-19 zhanghd@mochasoft.com.cn
*/
int temp = Integer.parseInt(t_boInsState);
String tagName = t_attachmentManager.getActiveTagName(t_taskId, t_boInsId, temp);
if(tagName.equalsIgnoreCase("Centralizedpurchasing"))
{
IEmpInstMgmtService mgmtService = (IEmpInstMgmtService)ServiceManager.getInstance().getService(com.mdcl.mocha.bpm.sdk.osm.IEmpInstMgmtService.class);
EmpInstBean ei = mgmtService.getEIById(t_eiID);
String orgName = ei.getOrginfo().getOrgname();
t_fileName = "(" + orgName + ")" + t_fileName;
}
/**
* 集中采购环节在附件名前加部门名 end
* jlbpm zhanghd 2007-09-19 zhanghd@mochasoft.com.cn
*/
t_Attachment.setBytes(t_item.get());
t_Attachment.setAttachmentType(t_fileType);
t_Attachment.setAttchmentName(t_fileName);
// 这里把临时附件上载到文件资源,只是单纯的上载,而没有把文件资源和表单进行关系绑定。在用户确定上载附件
// 后,才进行绑定关系操作。这么做,只需要读取一次I/O操作。
String resourceId = t_attachmentManager.uploadAttachmentResourceFile(t_Attachment);
t_map = setTempFileMap(resourceId, t_fileName, t_fileType,
t_beginNumber, t_realName, t_oneattachsize,
t_taskId, t_boInsId, request);
t_totalSize = t_totalSize + t_oneattachsize;
if (t_totalSize > FILE_TOTAL_SIZE_MAX) {
t_totalSize = t_totalSize - t_oneattachsize;
t_attachTotalSizeMore = "YES";
break;
}
t_tempSaveList.add(t_map); // 临时保存已经上传的附件
t_beginNumber += 1; // 序号加1
}
}
// 把新增附件放入session
t_getSession.setTotalSize(String.valueOf(t_totalSize));
t_getSession.setAttachTempList(t_tempSaveList);
} catch (FileUploadException e) {
t_sigleSizeMore = "YES";
request.setAttribute("ResultCheck", "F");
} catch (BPMBusinessListenerException e) {
} catch (BPMBusinessException e) {
} catch (NotFoundModuleException e) {
} catch (ExpiryException e) {
} catch (LicenseException e) {
} catch (Exception e) {
}
// 放入request
request.setAttribute("AttachUpload", t_getSession);
request.setAttribute("BoInsId", t_boInsId);
request.setAttribute("TaskId", t_taskId);
request.setAttribute("EiID", t_eiID);
request.setAttribute("BoInsState", t_boInsState);
// 附件总大小是否超过20M YES 超过 NO表示没有超过
request.setAttribute("TotalMoreThan", t_attachTotalSizeMore);
// 附件单个大小是否超过20M YES 超过 NO表示没有超过
request.setAttribute("SigleMoreThan", t_sigleSizeMore);
return mapping.findForward("SUCCESS");
}
/**
*
* 取得文件名和扩展名
* @param t_item
* @return
*/
private String getRealName(FileItem item){
String t_fullName = item.getName();
String t_realName = t_fullName.substring(t_fullName.lastIndexOf("\\") + 1);
return t_realName ;
}
/**
*
* 设置临时文件的属性
* @param t_fileName
* @param t_fileType
* @param t_beginNumber
* @param t_realName
* @param t_oneattachsize
* @param t_taskId
* @param t_boInsId
* @param request
* @return
* @throws Exception
*/
private Map setTempFileMap(String resourceId, String fileName, String fileType,
int beginNumber, String realName, long oneattachsize,
String taskId, String boInsId, HttpServletRequest request)
throws Exception {
Map t_map = new HashMap();
t_map.put("resourceId", resourceId);
t_map.put("FileName", fileName);
t_map.put("FileType", fileType);
t_map.put("ID", String.valueOf(beginNumber));
t_map.put("Name", realName);
t_map.put("Size", String.valueOf(oneattachsize));
String t_removeFlag = "";
t_removeFlag = getRemoveFlag(taskId, boInsId);
t_map.put("RemoveValue", t_removeFlag);
return t_map;
}
/**
* 获得附件可删除标识
*
* @param taskId
* 文档任务id
* @param boInsID
* 文种实例id
* @return 可删除标识
* @throws BPMBusinessException
* 异常
*/
private String getRemoveFlag(String taskId, String boInsID)
throws Exception {
IAttachmentManager t_attachment = (IAttachmentManager) ServiceManager
.getInstance().getService(IAttachmentManager.class);
try {
int t_num = t_attachment.isDelAttachment(boInsID);
String t_str = String.valueOf(t_num);
return t_str;
} catch (BPMBusinessException e) {
throw e;
}
}
/**
* 把流缓存到磁盘
*
* @param file
* 文件
* @param data
* 字节数组
*/
public void writeToFile(File file, byte[] data) {
OutputStream t_out = null;
try {
t_out = new FileOutputStream(file);
t_out.write(data);
t_out.close();
} catch (IOException e) {
} finally {
if (t_out != null) {
try {
t_out.close();
} catch (IOException e) {
}
}
}
}
private IAttachmentManager getServiceManagerInstance() {
ServiceManager t_servicemanager = ServiceManager.getInstance();
try {
return (IAttachmentManager) t_servicemanager
.getService(IAttachmentManager.class);
} catch (NotFoundModuleException e) {
} catch (ExpiryException e) {
} catch (LicenseException e) {
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -