| 
 | 
 
| 
 
 struts2没有提供自己的请求解析器,也就是说,struts2不会自己区处理multipart/form-data的请 
 
求,它需要调用其他请求解析器,将HTTP请求中的表单域解析出来,但struts2在原有的上传解析器 
 
上作了进一步封装,更进一步简化了文件上传 
 
 
以下是Ext上传form表单 
 
 
       function importExcel() { 
 
    var excelForm = new Ext.form.FormPanel({ 
 
              id : 'importExcelForm', 
 
              name : 'importExcelForm', 
 
              autoWidth : true, 
 
              height : 70, 
 
              frame : true, 
 
              labelWidth : 70, 
 
              fileUpload : true,  //上传文件,这个是必须的 
 
              enctype : 'multipart/form-data', //上传文件,这个是必须的 
 
              items : [{ 
 
                         xtype : 'textfield', 
 
                         id : 'excelFile', 
 
                         anchor : '100%', 
 
                         fieldLabel : 'Excel文件', 
 
                         name : 'excelFile', 
 
                         inputType : 'file', 
 
                         style : 'padding: 3px' 
 
                     }] 
 
           }); 
 
    var importExcelWin = new Ext.Window({ 
 
              height : 150, 
 
              width : 400, 
 
              id : 'importExcelWin', 
 
              name : 'importExcelWin', 
 
              bodyStyle : 'padding:5px 5px 5px 5px', 
 
              layout : 'form', 
 
              modal : true, 
 
              title : '导入Excel数据', 
 
              buttonAlign : 'center', 
 
              items : excelForm, 
 
              buttons : [{ 
 
                  text : '确定', 
 
                  handler : function add() { 
 
                     var flowDefinitionFileValue = Ext.getCmp('excelFile') 
 
                            .getValue(); 
 
                     var pos1 = flowDefinitionFileValue.lastIndexOf("\\") 
 
                            * 1; 
 
                     var fileNameTemp = flowDefinitionFileValue 
 
                            .substring(pos1 + 1); 
 
                     var pos2 = fileNameTemp.indexOf(".") * 1; 
 
                     var fileName = fileNameTemp.substring(0, pos2); 
 
                     var fileType = fileNameTemp.substring(pos2 + 1); 
 
                     var fileTypeUpperCase = fileType.toUpperCase(); 
 
                     if (!(fileTypeUpperCase == null || fileTypeUpperCase == '')) { 
 
                         if (!(fileTypeUpperCase == 'XLS')) { 
 
                             Ext.example.msg('提示:', '导入文件必须为xls文件!', 3); 
 
                            return; 
 
                         } 
 
                     } else { 
 
                         Ext.example.msg('提示:', '请输入导入xls文件!'); 
 
                         return 
 
                     } 
 
                     excelForm.form.submit({ 
 
                                waitMag : '正在导入数据请稍候', 
 
                                waitTitle : '提示', 
 
                                url : WWWROOT 
 
                                       + '/asset/importAssetExcel.action', 
 
                                method : 'post', 
 
                                params : { 
 
                                   excelFile : flowDefinitionFileValue, 
 
                                   checkId : id 
 
                                }, 
 
                                success : function(form, action) { 
 
                                   Ext.example.msg('提示:', '导入数据成功!'); 
 
                                   importExcelWin.close(); 
 
                                   Ext.getCmp("excelGrid").getStore() 
 
                                          .reload(); 
 
                                }, 
 
                                failure : function() { 
 
                                   Ext.example.msg('提示:', '导入数据失败!'); 
 
                                } 
 
                            }); 
 
                  } 
 
              }, { 
 
                  text : '关闭', 
 
                  handler : function close() { 
 
                     importExcelWin.close(); 
 
                  } 
 
              }] 
 
           }); 
 
    importExcelWin.show(); 
 
} 
 
 
java代码: 
 
 
package com.newsoft.aos.action.assetManager; 
 
 
import java.io.File; 
 
import java.io.FileInputStream; 
 
import java.io.FileOutputStream;  
 
  
 
public class AssetExcelAction extends BaseAction { 
 
    private File excelFile; 
 
 
    public void setExcelFile(File excelFile) { 
 
       this.excelFile = excelFile; 
 
    } 
 
 
    public File getExcelFile() { 
 
       return excelFile; 
 
    }  
 
    /** 
 
     * 上传文件 
 
     *  
 
     * 导出Excel文件 
 
     *  
 
     */ 
 
    public void importAssetExcel() { 
 
       String filePath = StrutsParamTools.getPraramValue("excelFile", ""); 
 
       AssetExcel temp = new AssetExcel(); 
 
       int begin = 2; 
 
       int end = 1000; 
 
       int sheet = 1; 
 
 
       try { 
 
           // 得到文件的扩展名 
 
           String t_ext = filePath.substring(filePath.lastIndexOf(".") + 1); 
 
           // 根据系统时间生成上传后保存的文件名 
 
           String prefix = String.valueOf(System.currentTimeMillis()); 
 
           // 保存的路径 
 
           String savePath = StrutsParamTools.getRequest().getRealPath("/") 
 
                  + "upload/" + prefix + "." + t_ext; 
 
           // 输出流 
 
           FileOutputStream fos = new FileOutputStream(savePath); 
 
           // 输入流 
 
           FileInputStream fis = new FileInputStream(getExcelFile()); 
 
           // 写入 
 
           byte[] buffer = new byte[1024]; 
 
           int len = 0; 
 
           while ((len = fis.read(buffer)) > 0) { 
 
              fos.write(buffer, 0, len); 
 
           } 
 
       } catch (Exception e) { 
 
           e.printStackTrace(); 
 
       } 
 
    } 
 
} 
 
 
解析excel文件: 
 
大体思路: 
 
页面(显示数据)--->点击导入--->选择文件--->后台处理(先上传,然后读取文件写入数据库)--->页 
 
面自动刷新显示所有数据. 
 
import jxl.Sheet; 
 
import jxl.Workboo        // // 获得输入流 
 
       InputSream is = new FileInputStream(excelFile); 
 
       // 定义excel工作簿,也就是在工作的sheet页面 
 
       Workbook wb; 
 
       // 资产Excel表格 
 
       AssetExcel assetExcel; 
 
 
       // 根据输入流获得工作簿 
 
       wb = Workbook.getWorkbook(is); 
 
 
       // 用于存储资产表格集合 
 
       List<AssetExcel> lists = new ArrayList<AssetExcel>(); 
 
 
       // 循环工作薄Excel中的sheet 
 
       for (int i = 0; i < sheet; i++) { 
 
           Sheet sh = wb.getSheet(i); 
 
           if (sh == null) { 
 
              break; 
 
           } 
 
           // 获取Sheet表中所包含的总行数 
 
           int e = sh.getRows() >= end ? end : sh.getRows(); 
 
           // 开始循环读取Excel表中的数据 
 
           // 外层循环循环行 
 
           for (int j = begin - 1; j < e; j++) { 
 
              if (sh.getCell(0, j).getContents() != null 
 
                     && !"".equals(sh.getCell(0, j).getContents())) { 
 
                  // 实例化一个资产excel表格信息 
 
                  assetExcel = new AssetExcel(); 
 
                  // sh.getRow(j) 获得一行的所有单元格 
 
                  // sh.getRow(j).length 列的数量 
 
                  int cols = sh.getRow(j).length; 
 
                  if (cols > 0 && sh.getCell(0, j) != null) { 
 
                     // sh.getCell(0, j) 第一个参数是列,第二个参数是行 
 
                     assetExcel.setAssetNo(sh.getCell(0, j).getContents() 
 
                            .trim()); 
 
                     assetExcel.setCheckId(checkId); 
 
                  } 
 
                  lists.add(assetExcel); 
 
              } 
 
           }  
 
        //以下是对数据库的操作 
 
           // 获得该盘点项下的所有盘点资产 
 
           List<AssetExcel> aes = objectDao.findByProperty(AssetExcel.class 
 
                  .getName(), "checkId", checkId); 
 
           // 删除该盘点下的资产 
 
           objectDao.deleteAll(aes); 
 
 
           // 保存 
 
           objectDao.saveAll(lists); |   
 
 
 
 |