SpringMVC Shiro 上传文件时,获取的是ShiroHttpServletRequest,如何转换成MultipartHttpServletRequest

  /**
   * 资料上传
   *
   * @param request
   * @return
   * @throws Exception
   */
  public JSONObject uploadFile(HttpServletRequest request, HttpServletResponse response) throws Exception {
    ShiroHttpServletRequest shiroRequest = (ShiroHttpServletRequest) request;
    CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
    MultipartHttpServletRequest multipartRequest = commonsMultipartResolver.resolveMultipart((HttpServletRequest) shiroRequest.getRequest());

    Iterator itr = multipartRequest.getFileNames();
    MultipartFile multipartFile = null;

    while (itr.hasNext()) {
      multipartFile = multipartRequest.getFile(itr.next());
      String fileRealName = String.valueOf(new Date().getTime());
      fileRealName = fileRealName + FileUtils.getSuffix(multipartFile.getOriginalFilename());

      HSSFWorkbook hssfWorkbook = new HSSFWorkbook(multipartFile.getInputStream());
      // xlsx
      //XSSFWorkbook xssfWorkbook = new XSSFWorkbook(multipartFile.getInputStream());

      if (hssfWorkbook != null) {
        for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
          HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
          // xlsx
          //XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);

          // 循环行Row
          for (int rowNum = 0; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
            if (rowNum == 0) {
              continue;
            }
            HSSFRow hssfRow = hssfSheet.getRow(rowNum);
            if (hssfRow == null || (hssfRow.getCell(0) == null && hssfRow.getCell(1) == null && hssfRow.getCell(2) == null && hssfRow.getCell(3) == null && hssfRow.getCell(4) == null
                    && hssfRow.getCell(5) == null && hssfRow.getCell(6) == null && hssfRow.getCell(7) == null && hssfRow.getCell(8) == null && hssfRow.getCell(9) == null)) {
              continue;
            }

          }
        }
      }
    }
    JSONObject obj = new JSONObject();
    obj.put("title", "123123");
    return obj;
  }
发现这个问题的大家凑合着看看,有更好的解决方法的亲,也请留下更好的方法。

 
  
 
 

你可能感兴趣的:(SpringMVC)