天天看點

multipartResolver

1. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
public class CardSecretForm {
private Long id;
private String cardsecret;
private Long upperid;
private MultipartFile fdata;
private String iscover; ................................
}
 @RequestMapping(params="method=importCardSecretListExcel")
   public void importCardSecretListExcel(CardSecretForm  cardSecretForm,HttpServletRequest request, HttpServletResponse response) throws IOException 
   {
String user=(String) request.getSession(true).getAttribute(MgrConstant.SESSION_KEY);
String iscover = cardSecretForm.getIscover();
  boolean cover = "1".equals(iscover);
    int ignoreRows = 1;
    int ignoreSheets = -1;
    Long upperid=cardSecretForm.getUpperid();
    InputStream in = null;
    if(cardSecretForm.getUpperid()!=null)
    {
    try 
    {
    in = cardSecretForm.getFdata().getInputStream();
    List<String[]> result = ExcelUtil.importExcel(new BufferedInputStream(in), ignoreRows, ignoreSheets);
    for(String[] array:result)
    { 
    String cardSecret=array[0];
    if(cardSecret!=null)
    { 
    
    CardSecretCondition   condition=new CardSecretCondition();
    condition.setCardsecret(cardSecret);
    condition.setUpperid(upperid);
    List<FcCardSecret> list=new ArrayList<FcCardSecret>();
try {
list = fcCardSecretService.getAllFcCardSecrets(condition,null);
} catch (Exception e2) {

e2.printStackTrace();
}
    if(list!=null&&list.size()>0)
    {
    if(cover)
    {
    //edit
    if(list!=null&&list.size()>0){
    for(FcCardSecret  secret:list){
    secret.setCardsecret(secret.getCardsecret());
    try 
    {
    fcCardSecretService.saveFcCardSecret(secret);
} 
    catch (Exception e) 
    {
    logger.error("edit fcBlackRoster failed!",e);
}
    
    }
    }
    
    }
    }
    else
    {
    //new
    FcOrderUpperInfo fcOrderUpperInfo=new FcOrderUpperInfo();
try {
fcOrderUpperInfo = fcOrderUpperInfoService.getById(upperid);
} catch (Exception e1) {

e1.printStackTrace();
}
    FcCardSecret fcCardSecret = new FcCardSecret();
    fcCardSecret.setCardsecret(cardSecret);
    fcCardSecret.setUpperid(upperid);
    fcCardSecret.setCreateTime(new Date());
    fcCardSecret.setUpdateTime(new Date());
    fcCardSecret.setModifier(user);
    fcCardSecret.setStatus(1);
    fcCardSecret.setUppername(fcOrderUpperInfo.getName());
    fcCardSecret.setStatus(0);//0啟用
                try 
                {
         
         fcCardSecretService.saveFcCardSecret(fcCardSecret);
}
                catch (Exception e) 
                {
logger.error("add importCardSecretListExcel failed!",e);
}
    }
    }
    }
    
} 
    catch (IOException e) 
    {
    logger.error("import list failed!",e);
}
    finally
    {
    if(in!=null)
    {
    try 
    {
in.close();
} 
    catch (IOException e) {}
    }
    }
    }
    response.sendRedirect("cardSecret.do?method=manage");
   }
3.
@SuppressWarnings({ "deprecation" })
public static List<String[]> importExcel(BufferedInputStream in, int ignoreRows, int ignoreSheets)
throws FileNotFoundException, IOException 
{
List<String[]> result = new ArrayList<String[]>();
int rowSize = 0;
// 打開HSSFWorkbook
POIFSFileSystem fs = null;
HSSFWorkbook wb = null;
HSSFCell cell = null;
try {
fs = new POIFSFileSystem(in);
wb = new HSSFWorkbook(fs);
for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++)
{
if(ignoreSheets!=-1&&sheetIndex + 1 > ignoreSheets)
break;

HSSFSheet st = wb.getSheetAt(sheetIndex);
// 第一行為标題,不取
for (int rowIndex = ignoreRows; rowIndex <= st.getLastRowNum(); rowIndex++) 
{
HSSFRow row = st.getRow(rowIndex);
if (row == null)
{
//空白行也記錄
result.add(new String[rowSize]);
continue;
}
//沒有指定索引的那一行,則不記錄
if (row.getLastCellNum() == -1)
{
result.add(null);
continue;
}
int tempRowSize = row.getLastCellNum() + 1;
if (tempRowSize > rowSize) 
{
rowSize = tempRowSize;
}
String[] values = new String[rowSize];
Arrays.fill(values, "");
for (short columnIndex = 0; columnIndex <= row.getLastCellNum(); columnIndex++) 
{
String value = "";
cell = row.getCell(columnIndex);
if (cell != null) 
{
switch (cell.getCellType()) 
{
case HSSFCell.CELL_TYPE_STRING:
value = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) 
{
Date date = cell.getDateCellValue();
if (date != null)
{
value = new SimpleDateFormat("yyyy-MM-dd")
.format(date);
}
else 
{
value = "";
}
}
else 
{
value = new DecimalFormat("0").format(cell
.getNumericCellValue());
}
break;
case HSSFCell.CELL_TYPE_FORMULA:
// 導入時如果為公式生成的資料則無值
if (!cell.getStringCellValue().equals(""))
{
value = cell.getStringCellValue();
} 
else
{
value = cell.getNumericCellValue() + "";
}
break;
case HSSFCell.CELL_TYPE_BLANK:
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
value = (cell.getBooleanCellValue() == true ? "Y"
: "N");
break;
default:
value = "";
}
}
values[columnIndex] = rightTrim(value);
}
result.add(values);
}
}
} catch (Exception e) {
e.printStackTrace();
}
finally
{
wb = null;
fs = null;
if(in!=null)
in.close();
}
return result;
}
4.
<!-- 上傳excel表單 -->
     <div id="uploadWindow" class="mini-window" title="導入卡密資訊" style="width:580px;" showModal="true" allowResize="true" allowDrag="true">
         <form id="fcCardSecretExcelForm" method="post" 
         action="cardSecret.do?method=importCardSecretListExcel"
         enctype="multipart/form-data"> 
         <table style="width:100%;">
                 <tr>
                 <td style="width:100px;"><font class="required">*</font>Excel檔案:</td>
                 <td style="width:150px;">
                 <input name="fdata" style="width:400px;" class="mini-htmlfile"  limitType="*.xls;*.xlsx" required="true" />
                 </td>
                  </tr>
                  <tr> 
                 <td style="width:100px;"><font class="required">*</font>上遊ID:</td>
                     <td style="width:150px;">
                         <input name="upperid" 
                         class="mini-text" style="width:400px;"  required="true"/>
                     </td>
                 </tr>
                 <tr> 
                 <td style="width:100px;"><font class="required">*</font>如果卡密重複:</td>
                     <td style="width:150px;">
                     <input name="iscover" style="width:400px;" class="mini-radiobuttonlist" 
               data="[{id: '0', text: '忽略'}, {id: '1', text: '覆寫'}]" required="true"/>
               </td>
               </tr> 
                 <tr>
                 <td><input type="submit"  value="上傳"/></td>
                 <td><input type="button"  οnclick="onUploadCancel()" value="取消"/></td>
                 </tr>
             </table>
         </form>
</div>5.
<script type="text/javascript">
     mini.parse();
     var uploadWindow = mini.get("uploadWindow");
     var fcCardSecretExcelForm = new mini.Form("#fcCardSecretExcelForm");
     function importExcel() 
     {
     uploadWindow.show();
     
     }


     function onUploadCancel(){
         uploadWindow.hide();
    }     
 </script>