org.springframework.web.multipart.MultipartException Java Examples
The following examples show how to use
org.springframework.web.multipart.MultipartException.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WxArgumentResolver.java From FastBootWeixin with Apache License 2.0 | 8 votes |
@Override protected void handleMissingValue(String name, MethodParameter parameter, NativeWebRequest request) throws Exception { HttpServletRequest servletRequest = request.getNativeRequest(HttpServletRequest.class); if (MultipartResolutionDelegate.isMultipartArgument(parameter)) { if (!MultipartResolutionDelegate.isMultipartRequest(servletRequest)) { throw new MultipartException("Current request is not a multipart request"); } else { throw new MissingServletRequestPartException(name); } } else { throw new MissingServletRequestParameterException(name, parameter.getNestedParameterType().getSimpleName()); } }
Example #2
Source File: StandardMultipartHttpServletRequest.java From lams with GNU General Public License v2.0 | 7 votes |
private void parseRequest(HttpServletRequest request) { try { Collection<Part> parts = request.getParts(); this.multipartParameterNames = new LinkedHashSet<String>(parts.size()); MultiValueMap<String, MultipartFile> files = new LinkedMultiValueMap<String, MultipartFile>(parts.size()); for (Part part : parts) { String disposition = part.getHeader(CONTENT_DISPOSITION); String filename = extractFilename(disposition); if (filename == null) { filename = extractFilenameWithCharset(disposition); } if (filename != null) { files.add(part.getName(), new StandardMultipartFile(part, filename)); } else { this.multipartParameterNames.add(part.getName()); } } setMultipartFiles(files); } catch (Throwable ex) { throw new MultipartException("Could not parse multipart servlet request", ex); } }
Example #3
Source File: RequestParamMethodArgumentResolver.java From java-technology-stack with MIT License | 6 votes |
@Override protected void handleMissingValue(String name, MethodParameter parameter, NativeWebRequest request) throws Exception { HttpServletRequest servletRequest = request.getNativeRequest(HttpServletRequest.class); if (MultipartResolutionDelegate.isMultipartArgument(parameter)) { if (servletRequest == null || !MultipartResolutionDelegate.isMultipartRequest(servletRequest)) { throw new MultipartException("Current request is not a multipart request"); } else { throw new MissingServletRequestPartException(name); } } else { throw new MissingServletRequestParameterException(name, parameter.getNestedParameterType().getSimpleName()); } }
Example #4
Source File: FileUploadController.java From ChengFeng1.5 with MIT License | 6 votes |
@ApiOperation(value = "文件上传",notes = "批量上传文件",produces = "multipart/form-data", response = ResponseResult.class,httpMethod = "POST") @PostMapping("/uploads") @ResponseBody public ResponseResult uploadFiles(@RequestParam("files") MultipartFile[] files){ if(files==null||files.length<1){ throw new MultipartException(ChengfengConstant.File.UPLOAD_EMPTY_ERROR); } try { StringBuffer stringBuffer=new StringBuffer(); for (int i=0;i<files.length;i++){ if (i!=files.length-1){ stringBuffer.append(saveFile(files[i])).append(","); }else{ stringBuffer.append(saveFile(files[i])); } } return ResponseResult.createBySuccess("图片上传成功",stringBuffer.toString()); } catch (IOException e) { throw new MultipartException(ChengfengConstant.File.UPLOAD_FAILURE); } }
Example #5
Source File: DispatcherServlet.java From spring4-understanding with Apache License 2.0 | 6 votes |
/** * Convert the request into a multipart request, and make multipart resolver available. * <p>If no multipart resolver is set, simply use the existing request. * @param request current HTTP request * @return the processed request (multipart wrapper if necessary) * @see MultipartResolver#resolveMultipart */ protected HttpServletRequest checkMultipart(HttpServletRequest request) throws MultipartException { if (this.multipartResolver != null && this.multipartResolver.isMultipart(request)) { if (WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class) != null) { logger.debug("Request is already a MultipartHttpServletRequest - if not in a forward, " + "this typically results from an additional MultipartFilter in web.xml"); } else if (request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE) instanceof MultipartException) { logger.debug("Multipart resolution failed for current request before - " + "skipping re-resolution for undisturbed error rendering"); } else { return this.multipartResolver.resolveMultipart(request); } } // If not returned before: return original request. return request; }
Example #6
Source File: StandardMultipartHttpServletRequest.java From spring4-understanding with Apache License 2.0 | 6 votes |
private void parseRequest(HttpServletRequest request) { try { Collection<Part> parts = request.getParts(); this.multipartParameterNames = new LinkedHashSet<String>(parts.size()); MultiValueMap<String, MultipartFile> files = new LinkedMultiValueMap<String, MultipartFile>(parts.size()); for (Part part : parts) { String disposition = part.getHeader(CONTENT_DISPOSITION); String filename = extractFilename(disposition); if (filename == null) { filename = extractFilenameWithCharset(disposition); } if (filename != null) { files.add(part.getName(), new StandardMultipartFile(part, filename)); } else { this.multipartParameterNames.add(part.getName()); } } setMultipartFiles(files); } catch (Exception ex) { throw new MultipartException("Could not parse multipart servlet request", ex); } }
Example #7
Source File: RequestPartServletServerHttpRequest.java From spring-analysis-note with MIT License | 6 votes |
@Override public InputStream getBody() throws IOException { if (this.multipartRequest instanceof StandardMultipartHttpServletRequest) { try { return this.multipartRequest.getPart(this.partName).getInputStream(); } catch (Exception ex) { throw new MultipartException("Could not parse multipart servlet request", ex); } } else { MultipartFile file = this.multipartRequest.getFile(this.partName); if (file != null) { return file.getInputStream(); } else { String paramValue = this.multipartRequest.getParameter(this.partName); return new ByteArrayInputStream(paramValue.getBytes(determineCharset())); } } }
Example #8
Source File: RequestPartServletServerHttpRequest.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Override public InputStream getBody() throws IOException { if (this.multipartRequest instanceof StandardMultipartHttpServletRequest) { try { return this.multipartRequest.getPart(this.partName).getInputStream(); } catch (Exception ex) { throw new MultipartException("Could not parse multipart servlet request", ex); } } else { MultipartFile file = this.multipartRequest.getFile(this.partName); if (file != null) { return file.getInputStream(); } else { String paramValue = this.multipartRequest.getParameter(this.partName); return new ByteArrayInputStream(paramValue.getBytes(determineEncoding())); } } }
Example #9
Source File: FastDfsClientUtil.java From ChengFeng1.5 with MIT License | 6 votes |
public static String uploadFiles(MultipartFile[] files){ List<String> paths= Lists.newArrayList(); if (files!=null&&files.length>0){ for (int i=0;i<files.length;i++){ if(files[i].isEmpty()){ throw new MultipartException(ChengfengConstant.File.UPLOAD_EMPTY_ERROR); } try { String path = saveFile(files[i]); paths.add(path); } catch (IOException e) { throw new MultipartException(ChengfengConstant.File.UPLOAD_FAILURE); } } } return String.join(",",paths);//java 8 }
Example #10
Source File: ComplexPortletApplicationContext.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Override public MultipartActionRequest resolveMultipart(ActionRequest request) throws MultipartException { if (request.getAttribute("fail") != null) { throw new MaxUploadSizeExceededException(1000); } if (request instanceof MultipartActionRequest) { throw new IllegalStateException("Already a multipart request"); } if (request.getAttribute("resolved") != null) { throw new IllegalStateException("Already resolved"); } request.setAttribute("resolved", Boolean.TRUE); MultiValueMap<String, MultipartFile> files = new LinkedMultiValueMap<String, MultipartFile>(); files.set("someFile", new MockMultipartFile("someFile", "someContent".getBytes())); Map<String, String[]> params = new HashMap<String, String[]>(); params.put("someParam", new String[] {"someParam"}); return new DefaultMultipartActionRequest(request, files, params, Collections.<String, String>emptyMap()); }
Example #11
Source File: RequestParamMethodArgumentResolver.java From lams with GNU General Public License v2.0 | 6 votes |
@Override protected void handleMissingValue(String name, MethodParameter parameter, NativeWebRequest request) throws Exception { HttpServletRequest servletRequest = request.getNativeRequest(HttpServletRequest.class); if (MultipartResolutionDelegate.isMultipartArgument(parameter)) { if (!MultipartResolutionDelegate.isMultipartRequest(servletRequest)) { throw new MultipartException("Current request is not a multipart request"); } else { throw new MissingServletRequestPartException(name); } } else { throw new MissingServletRequestParameterException(name, parameter.getNestedParameterType().getSimpleName()); } }
Example #12
Source File: StandardMultipartHttpServletRequest.java From spring-analysis-note with MIT License | 6 votes |
@Override public HttpHeaders getMultipartHeaders(String paramOrFileName) { try { Part part = getPart(paramOrFileName); if (part != null) { HttpHeaders headers = new HttpHeaders(); for (String headerName : part.getHeaderNames()) { headers.put(headerName, new ArrayList<>(part.getHeaders(headerName))); } return headers; } else { return null; } } catch (Throwable ex) { throw new MultipartException("Could not access multipart servlet request", ex); } }
Example #13
Source File: StandardMultipartHttpServletRequest.java From lams with GNU General Public License v2.0 | 6 votes |
@Override public HttpHeaders getMultipartHeaders(String paramOrFileName) { try { Part part = getPart(paramOrFileName); if (part != null) { HttpHeaders headers = new HttpHeaders(); for (String headerName : part.getHeaderNames()) { headers.put(headerName, new ArrayList<String>(part.getHeaders(headerName))); } return headers; } else { return null; } } catch (Throwable ex) { throw new MultipartException("Could not access multipart servlet request", ex); } }
Example #14
Source File: FastDfsClientUtil.java From DrivingAgency with MIT License | 6 votes |
public static String uploadFiles(MultipartFile[] files){ List<String> paths= Lists.newArrayList(); if (files!=null&&files.length>0){ for (int i=0;i<files.length;i++){ if(files[i].isEmpty()){ throw new MultipartException(DrivingConstant.File.UPLOAD_EMPTY_ERROR); } try { String path = saveFile(files[i]); paths.add(path); } catch (IOException e) { throw new MultipartException(DrivingConstant.File.UPLOAD_FAILURE); } } } return String.join(",",paths);//java 8 }
Example #15
Source File: RequestPartServletServerHttpRequest.java From lams with GNU General Public License v2.0 | 6 votes |
@Override public InputStream getBody() throws IOException { if (this.multipartRequest instanceof StandardMultipartHttpServletRequest) { try { return this.multipartRequest.getPart(this.partName).getInputStream(); } catch (Exception ex) { throw new MultipartException("Could not parse multipart servlet request", ex); } } else { MultipartFile file = this.multipartRequest.getFile(this.partName); if (file != null) { return file.getInputStream(); } else { String paramValue = this.multipartRequest.getParameter(this.partName); return new ByteArrayInputStream(paramValue.getBytes(determineEncoding())); } } }
Example #16
Source File: RequestPartServletServerHttpRequest.java From java-technology-stack with MIT License | 6 votes |
@Override public InputStream getBody() throws IOException { if (this.multipartRequest instanceof StandardMultipartHttpServletRequest) { try { return this.multipartRequest.getPart(this.partName).getInputStream(); } catch (Exception ex) { throw new MultipartException("Could not parse multipart servlet request", ex); } } else { MultipartFile file = this.multipartRequest.getFile(this.partName); if (file != null) { return file.getInputStream(); } else { String paramValue = this.multipartRequest.getParameter(this.partName); return new ByteArrayInputStream(paramValue.getBytes(determineCharset())); } } }
Example #17
Source File: RequestParamMethodArgumentResolver.java From spring-analysis-note with MIT License | 6 votes |
@Override protected void handleMissingValue(String name, MethodParameter parameter, NativeWebRequest request) throws Exception { HttpServletRequest servletRequest = request.getNativeRequest(HttpServletRequest.class); if (MultipartResolutionDelegate.isMultipartArgument(parameter)) { if (servletRequest == null || !MultipartResolutionDelegate.isMultipartRequest(servletRequest)) { throw new MultipartException("Current request is not a multipart request"); } else { throw new MissingServletRequestPartException(name); } } else { throw new MissingServletRequestParameterException(name, parameter.getNestedParameterType().getSimpleName()); } }
Example #18
Source File: StandardMultipartHttpServletRequest.java From java-technology-stack with MIT License | 6 votes |
@Override public HttpHeaders getMultipartHeaders(String paramOrFileName) { try { Part part = getPart(paramOrFileName); if (part != null) { HttpHeaders headers = new HttpHeaders(); for (String headerName : part.getHeaderNames()) { headers.put(headerName, new ArrayList<>(part.getHeaders(headerName))); } return headers; } else { return null; } } catch (Throwable ex) { throw new MultipartException("Could not access multipart servlet request", ex); } }
Example #19
Source File: MultipartResolutionDelegate.java From lams with GNU General Public License v2.0 | 5 votes |
private static MultipartHttpServletRequest adaptToMultipartHttpServletRequest(HttpServletRequest request) { if (servletPartClass != null) { // Servlet 3.0 available .. return new StandardMultipartHttpServletRequest(request); } throw new MultipartException("Expected MultipartHttpServletRequest: is a MultipartResolver configured?"); }
Example #20
Source File: StandardMultipartHttpServletRequest.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Override public String getMultipartContentType(String paramOrFileName) { try { Part part = getPart(paramOrFileName); return (part != null ? part.getContentType() : null); } catch (Exception ex) { throw new MultipartException("Could not access multipart servlet request", ex); } }
Example #21
Source File: CosMultipartResolver.java From Lottery with GNU General Public License v2.0 | 5 votes |
public MultipartHttpServletRequest resolveMultipart( HttpServletRequest request) throws MultipartException { try { CosMultipartRequest multipartRequest = newMultipartRequest(request); if (logger.isDebugEnabled()) { Set<String> fileNames = multipartRequest.getFileNames(); for (String fileName : fileNames) { File file = multipartRequest.getFile(fileName); logger.debug("Found multipart file '" + fileName + "' of size " + (file != null ? file.length() : 0) + " bytes with original filename [" + multipartRequest.getOriginalFileName(fileName) + "]" + (file != null ? "stored at [" + file.getAbsolutePath() + "]" : "empty")); } } return new CosMultipartHttpServletRequest(request, multipartRequest); } catch (IOException ex) { // Unfortunately, COS always throws an IOException, // so we need to check the error message here! if (ex.getMessage().indexOf("exceeds limit") != -1) { throw new MaxUploadSizeExceededException(this.maxUploadSize, ex); } else { throw new MultipartException( "Could not parse multipart request", ex); } } }
Example #22
Source File: DispatcherPortlet.java From spring4-understanding with Apache License 2.0 | 5 votes |
/** * Convert the request into a multipart request, and make multipart resolver available. * If no multipart resolver is set, simply use the existing request. * @param request current HTTP request * @return the processed request (multipart wrapper if necessary) */ protected ActionRequest checkMultipart(ActionRequest request) throws MultipartException { if (this.multipartResolver != null && this.multipartResolver.isMultipart(request)) { if (request instanceof MultipartActionRequest) { logger.debug("Request is already a MultipartActionRequest - probably in a forward"); } else { return this.multipartResolver.resolveMultipart(request); } } // If not returned before: return original request. return request; }
Example #23
Source File: StandardMultipartHttpServletRequest.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Override public HttpHeaders getMultipartHeaders(String paramOrFileName) { try { Part part = getPart(paramOrFileName); if (part != null) { HttpHeaders headers = new HttpHeaders(); for (String headerName : part.getHeaderNames()) { headers.put(headerName, new ArrayList<String>(part.getHeaders(headerName))); } return headers; } else { return null; } } catch (Exception ex) { throw new MultipartException("Could not access multipart servlet request", ex); } }
Example #24
Source File: DispatcherServlet.java From lams with GNU General Public License v2.0 | 5 votes |
/** * Check "javax.servlet.error.exception" attribute for a multipart exception. */ private boolean hasMultipartException(HttpServletRequest request) { Throwable error = (Throwable) request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE); while (error != null) { if (error instanceof MultipartException) { return true; } error = error.getCause(); } return false; }
Example #25
Source File: DispatcherServlet.java From lams with GNU General Public License v2.0 | 5 votes |
/** * Convert the request into a multipart request, and make multipart resolver available. * <p>If no multipart resolver is set, simply use the existing request. * @param request current HTTP request * @return the processed request (multipart wrapper if necessary) * @see MultipartResolver#resolveMultipart */ protected HttpServletRequest checkMultipart(HttpServletRequest request) throws MultipartException { if (this.multipartResolver != null && this.multipartResolver.isMultipart(request)) { if (WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class) != null) { logger.debug("Request is already a MultipartHttpServletRequest - if not in a forward, " + "this typically results from an additional MultipartFilter in web.xml"); } else if (hasMultipartException(request) ) { logger.debug("Multipart resolution failed for current request before - " + "skipping re-resolution for undisturbed error rendering"); } else { try { return this.multipartResolver.resolveMultipart(request); } catch (MultipartException ex) { if (request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE) != null) { logger.debug("Multipart resolution failed for error dispatch", ex); // Keep processing error dispatch with regular request handle below } else { throw ex; } } } } // If not returned before: return original request. return request; }
Example #26
Source File: GlobalExceptionHandler.java From paascloud-master with Apache License 2.0 | 5 votes |
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ResponseBody @ExceptionHandler(MultipartException.class) public Wrapper multipartException(Throwable t) { log.error("上传文件, 出现错误={}", t.getMessage(), t); return WrapMapper.wrap(Wrapper.ERROR_CODE, "文件超过限制"); }
Example #27
Source File: RestControllerAdviceHandler.java From nimrod with MIT License | 5 votes |
@ExceptionHandler(MultipartException.class) public ResponseEntity<FailureEntity> sizeLimitExceededExceptionHandler(HttpServletRequest httpServletRequest, Throwable throwable) { HttpStatus httpStatus = getStatus(httpServletRequest); FailureEntity fm = failureEntity.i18n("file.upload_fail"); if (throwable instanceof MaxUploadSizeExceededException) { String maxFileSize = DataSizeUtil.pretty(DataSize.parse((String) dictionaryService.get("FILE", "MAX_FILE_SIZE")).toBytes()); String maxRequestSize = DataSizeUtil.pretty(DataSize.parse((String) dictionaryService.get("FILE", "MAX_REQUEST_SIZE")).toBytes()); fm = failureEntity.i18n("file.upload_fail_max_upload_size_exceeded", maxFileSize, maxRequestSize); } throwable.printStackTrace(); return new ResponseEntity<>(fm, httpStatus); }
Example #28
Source File: RequestParamMethodArgumentResolverTests.java From java-technology-stack with MIT License | 5 votes |
@Test(expected = MultipartException.class) public void noMultipartContent() throws Exception { request.setMethod("POST"); MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile.class); resolver.resolveArgument(param, null, webRequest, null); fail("Expected exception: no multipart content"); }
Example #29
Source File: MultipartAdviceTrait.java From problem-spring-web with MIT License | 5 votes |
@API(status = INTERNAL) @ExceptionHandler default ResponseEntity<Problem> handleMultipart( final MultipartException exception, final NativeWebRequest request) { return create(Status.BAD_REQUEST, exception, request); }
Example #30
Source File: BootStandardServletMultipartResolver.java From onetwo with Apache License 2.0 | 5 votes |
@Override public MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException { if(maxUploadSize>=0){ long requestSize = RequestUtils.getContentLength(request); if(requestSize!=-1 && requestSize>maxUploadSize){ throw new MaxUploadSizeExceededException(maxUploadSize); } } try { return super.resolveMultipart(request); } catch (MultipartException e) { FileSizeLimitExceededException fsee = LangUtils.getCauseException(e, FileSizeLimitExceededException.class); if (fsee!=null) { throw new UploadFileSizeLimitExceededException(fsee); } else { throw new BaseException(WebErrors.UPLOAD, e); } } }