• fileupload 上传execl文件的一些操作



    OABaseReadExeclDataAction .class
    包含创建临时文件目录,基本校验,取属性值,处理乱码,基类
    这里在上传文件是execl并且需要读取的话,需要把fileitem对象转换成file,处理方式是创建临时目录,用流写入到一个临时
    目录中
    1. package com.dadi.oa.web.action;
    2. import java.io.File;
    3. import java.util.HashMap;
    4. import java.util.Iterator;
    5. import java.util.List;
    6. import java.util.Map;
    7. import javax.servlet.http.HttpServletRequest;
    8. import javax.servlet.http.HttpServletResponse;
    9. import org.apache.commons.fileupload.FileItem;
    10. import org.apache.commons.fileupload.FileUpload;
    11. import org.apache.commons.fileupload.servlet.ServletFileUpload;
    12. import org.apache.commons.fileupload.servlet.ServletRequestContext;
    13. import org.apache.commons.io.FilenameUtils;
    14. import org.apache.commons.logging.Log;
    15. import org.apache.commons.logging.LogFactory;
    16. import org.apache.struts.action.ActionForm;
    17. import org.apache.struts.action.ActionForward;
    18. import org.apache.struts.action.ActionMapping;
    19. import org.springframework.web.context.WebApplicationContext;
    20. import org.springframework.web.context.support.WebApplicationContextUtils;
    21. import com.dadi.oa.pojo.Users;
    22. import com.dadi.oa.service.ReadExeclDataService;
    23. import com.dadi.oa.system.ConstantsUtil;
    24. import com.dadi.oa.util.RequestUtil;
    25. import com.dadi.oa.util.StringUtil;
    26. import com.dadi.oa.util.UploadUtil;
    27. /**
    28. * 读取execl action基类,处理一些通用操作
    29. * @author ao.ouyang
    30. *
    31. */
    32. public class OABaseReadExeclDataAction extends OABaseAction {
    33. private Log logger = LogFactory.getLog(this.getClass());
    34. protected ReadExeclDataService readExeclDataService;
    35. //默认读取execl的临时目录
    36. protected String tempReadExeclDir;
    37. //封装了form表单属性的集合
    38. protected Map<String,String> formFieldItems;
    39. protected File readExeclFile;
    40. private File tempDirectoryFile;
    41. public void setReadExeclDataService(ReadExeclDataService readExeclDataService) {
    42. this.readExeclDataService = readExeclDataService;
    43. }
    44. public void setTempReadExeclDir(String tempReadExeclDir) {
    45. this.tempReadExeclDir = tempReadExeclDir;
    46. }
    47. /**
    48. * 初始化临时文件目录
    49. */
    50. public void initDir(){
    51. formFieldItems = new HashMap<String, String>();
    52. String nativeWebAppFoldPath =((WebApplicationContext)ConstantsUtil.beanFactory).getServletContext().getRealPath("/");
    53. tempDirectoryFile = new File(nativeWebAppFoldPath + tempReadExeclDir);
    54. // 文件夹不存在就自动创建
    55. if (!tempDirectoryFile.isDirectory()) {
    56. tempDirectoryFile.mkdirs();
    57. }
    58. }
    59. @Override
    60. protected ActionForward dispatchMethod(ActionMapping mapping,
    61. ActionForm form, HttpServletRequest request,
    62. HttpServletResponse response, String name) throws Exception {
    63. // TODO Auto-generated method stub
    64. String resultStr=processFileUpload(request);
    65. if(!resultStr.equalsIgnoreCase("success")){
    66. request.setAttribute("info",resultStr);
    67. request.setAttribute("closepage", "yes");
    68. return mapping.findForward("oapromptinfo");
    69. }
    70. return super.dispatchMethod(mapping, form, request, response, name);
    71. }
    72. /**
    73. * 处理请求文件
    74. * @param request
    75. * @return
    76. */
    77. public String processFileUpload(HttpServletRequest request) throws Exception {
    78. String resultStr = "success";
    79. try {
    80. if(!FileUpload.isMultipartContent(new ServletRequestContext(request))){
    81. throw new Exception("操作异常,请重新再试!!!");
    82. }
    83. ServletFileUpload fileUpload = UploadUtil.getFileUploadObject(10);
    84. List fileItems = null;
    85. FileItem readItem = null;
    86. fileItems = fileUpload.parseRequest(request);
    87. for (Iterator it = fileItems.iterator(); it.hasNext();) {
    88. FileItem item = (FileItem) it.next();
    89. if (!item.isFormField()) {
    90. readItem = item;
    91. }else{
    92. formFieldItems.put(item.getFieldName(),item.getString("UTF-8"));
    93. }
    94. }
    95. //校验文件
    96. checkExecl(readItem);
    97. try {
    98. readExeclFile = new File(tempDirectoryFile.getAbsolutePath(), readItem.getName().substring(readItem.getName().lastIndexOf("\")+1));
    99. readItem.write(readExeclFile);
    100. readItem.delete();
    101. } catch (Exception e) {
    102. e.printStackTrace();
    103. throw new Exception("文件转换异常,请检查!!!");
    104. }
    105. } catch (Exception e) {
    106. e.printStackTrace();
    107. resultStr=e.getMessage();
    108. logger.info("================ERROR:OABaseReadExeclDataAction.executeSub()"+e.getMessage());
    109. }
    110. return resultStr;
    111. }
    112. /**
    113. * 校验文件
    114. * @throws Exception
    115. */
    116. private void checkExecl(FileItem readItem) throws Exception{
    117. logger.info("==========OABaseReadExeclDataAction.checkExecl() begin===========");
    118. String inputFileName = readItem.getName();
    119. long inputFileSize = readItem.getSize();
    120. if(StringUtil.isEmptyString(inputFileName)){
    121. throw new Exception("系统找不到文件,请确定文件路径!!!");
    122. }
    123. if(!(inputFileSize > 0)){
    124. throw new Exception("系统找不到文件或文件没有内容!!!");
    125. }
    126. String inputFileExtension = FilenameUtils.getExtension(inputFileName).toLowerCase();
    127. if("".equalsIgnoreCase(inputFileExtension)){
    128. throw new Exception("文件无后缀名,请选择正确的文件上传!!!");
    129. }
    130. if("xls,xlsx".indexOf(inputFileExtension)<0){
    131. throw new Exception("请上传execl文档格式,格式如:xls,xlxs;请选择正确的文件上传!!!");
    132. }
    133. logger.info("===========OABaseReadExeclDataAction.checkExecl() end===========");
    134. }
    135. }

  • 相关阅读:
    ActionScript 条件编译
    FLASH通讯小结
    关于ob_start()
    剖析PHP中的输出缓冲
    Ext.app.controller的refs
    php多线程解决之stream_socket_client
    谷歌的JQuery库
    PHP计划任务之关闭浏览器后仍然继续执行的函数
    关于php调用可执行程序,在后台运行而不让页面等待
    把预定义的字符串转化为html标签
  • 原文地址:https://www.cnblogs.com/signheart/p/6595663.html
Copyright © 2020-2023  润新知