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

来源:互联网 发布:华夏幸福和知合控股 编辑:程序博客网 时间:2024/05/17 00:00
<pre style="font-family: 宋体; font-size: 9pt; background-color: rgb(255, 255, 255);"><pre name="code" class="java">  /**   * 资料上传   *   * @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<String> 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;  }
发现这个问题的大家凑合着看看,有更好的解决方法的亲,也请留下更好的方法。

                                             
1 0
原创粉丝点击