OABaseReadExeclDataAction .class
包含创建临时文件目录,基本校验,取属性值,处理乱码,基类
这里在上传文件是execl并且需要读取的话,需要把fileitem对象转换成file,处理方式是创建临时目录,用流写入到一个临时
目录中
package com.dadi.oa.web.action;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.servlet.ServletRequestContext;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.dadi.oa.pojo.Users;
import com.dadi.oa.service.ReadExeclDataService;
import com.dadi.oa.system.ConstantsUtil;
import com.dadi.oa.util.RequestUtil;
import com.dadi.oa.util.StringUtil;
import com.dadi.oa.util.UploadUtil;
/**
* 读取execl action基类,处理一些通用操作
* @author ao.ouyang
*
*/
public class OABaseReadExeclDataAction extends OABaseAction {
private Log logger = LogFactory.getLog(this.getClass());
protected ReadExeclDataService readExeclDataService;
//默认读取execl的临时目录
protected String tempReadExeclDir;
//封装了form表单属性的集合
protected Map<String,String> formFieldItems;
protected File readExeclFile;
private File tempDirectoryFile;
public void setReadExeclDataService(ReadExeclDataService readExeclDataService) {
this.readExeclDataService = readExeclDataService;
}
public void setTempReadExeclDir(String tempReadExeclDir) {
this.tempReadExeclDir = tempReadExeclDir;
}
/**
* 初始化临时文件目录
*/
public void initDir(){
formFieldItems = new HashMap<String, String>();
String nativeWebAppFoldPath =((WebApplicationContext)ConstantsUtil.beanFactory).getServletContext().getRealPath("/");
tempDirectoryFile = new File(nativeWebAppFoldPath + tempReadExeclDir);
// 文件夹不存在就自动创建
if (!tempDirectoryFile.isDirectory()) {
tempDirectoryFile.mkdirs();
}
}
@Override
protected ActionForward dispatchMethod(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response, String name) throws Exception {
// TODO Auto-generated method stub
String resultStr=processFileUpload(request);
if(!resultStr.equalsIgnoreCase("success")){
request.setAttribute("info",resultStr);
request.setAttribute("closepage", "yes");
return mapping.findForward("oapromptinfo");
}
return super.dispatchMethod(mapping, form, request, response, name);
}
/**
* 处理请求文件
* @param request
* @return
*/
public String processFileUpload(HttpServletRequest request) throws Exception {
String resultStr = "success";
try {
if(!FileUpload.isMultipartContent(new ServletRequestContext(request))){
throw new Exception("操作异常,请重新再试!!!");
}
ServletFileUpload fileUpload = UploadUtil.getFileUploadObject(10);
List fileItems = null;
FileItem readItem = null;
fileItems = fileUpload.parseRequest(request);
for (Iterator it = fileItems.iterator(); it.hasNext();) {
FileItem item = (FileItem) it.next();
if (!item.isFormField()) {
readItem = item;
}else{
formFieldItems.put(item.getFieldName(),item.getString("UTF-8"));
}
}
//校验文件
checkExecl(readItem);
try {
readExeclFile = new File(tempDirectoryFile.getAbsolutePath(), readItem.getName().substring(readItem.getName().lastIndexOf("\")+1));
readItem.write(readExeclFile);
readItem.delete();
} catch (Exception e) {
e.printStackTrace();
throw new Exception("文件转换异常,请检查!!!");
}
} catch (Exception e) {
e.printStackTrace();
resultStr=e.getMessage();
logger.info("================ERROR:OABaseReadExeclDataAction.executeSub()"+e.getMessage());
}
return resultStr;
}
/**
* 校验文件
* @throws Exception
*/
private void checkExecl(FileItem readItem) throws Exception{
logger.info("==========OABaseReadExeclDataAction.checkExecl() begin===========");
String inputFileName = readItem.getName();
long inputFileSize = readItem.getSize();
if(StringUtil.isEmptyString(inputFileName)){
throw new Exception("系统找不到文件,请确定文件路径!!!");
}
if(!(inputFileSize > 0)){
throw new Exception("系统找不到文件或文件没有内容!!!");
}
String inputFileExtension = FilenameUtils.getExtension(inputFileName).toLowerCase();
if("".equalsIgnoreCase(inputFileExtension)){
throw new Exception("文件无后缀名,请选择正确的文件上传!!!");
}
if("xls,xlsx".indexOf(inputFileExtension)<0){
throw new Exception("请上传execl文档格式,格式如:xls,xlxs;请选择正确的文件上传!!!");
}
logger.info("===========OABaseReadExeclDataAction.checkExecl() end===========");
}
}