Springmvc文件上传监听详解编程语言

spring mvc CommonsMultipartResolver 文件上传监听。

/** 
  
  * 重写 parseRequest方法 监听 
  
  */ 
  
 @Override 
  
 protected MultipartParsingResult parseRequest(HttpServletRequest request) 
  
   throws MultipartException { 
  
  System.out.println("MultipartParsingResult:--------------------------"+request); 
  
  String encoding = determineEncoding(request); 
  
  FileUpload fileUpload = prepareFileUpload(encoding); 
  
  fileUpload.setFileSizeMax(-1); 
  
   final HttpSession session = request.getSession(); 
  
   fileUpload.setProgressListener(new ProgressListener() { 
  
    @Override 
  
    public void update(long pBytesRead, long pContentLength, 
  
      int pItems) { 
  
     try { 
  
      Thread.sleep(5); 
  
     } catch (InterruptedException e) { 
  
      e.printStackTrace(); 
  
     } 
  
     session.setAttribute("pBytesRead", pBytesRead); 
  
    } 
  
   }); 
  
  try { 
  
   List<FileItem> fileItems = ((ServletFileUpload) fileUpload).parseRequest(request); 
  
   return parseFileItems(fileItems, encoding); 
  
  } catch (FileUploadBase.SizeLimitExceededException ex) { 
  
   throw new MaxUploadSizeExceededException(fileUpload.getSizeMax(), 
  
     ex); 
  
  } catch (FileUploadException ex) { 
  
   throw new MultipartException( 
  
     "Could not parse multipart servlet request", ex); 
  
  } 
  
 }

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/8018.html

(0)
上一篇 2021年7月18日
下一篇 2021年7月18日

相关推荐

发表回复

登录后才能评论