Search in sources :

Example 6 with MultipartRequest

use of org.springframework.web.multipart.MultipartRequest in project spring-framework by spring-projects.

the class ServletRequestDataBinder method bind.

/**
 * Bind the parameters of the given request to this binder's target,
 * also binding multipart files in case of a multipart request.
 * <p>This call can create field errors, representing basic binding
 * errors like a required field (code "required"), or type mismatch
 * between value and bean property (code "typeMismatch").
 * <p>Multipart files are bound via their parameter name, just like normal
 * HTTP parameters: i.e. "uploadedFile" to an "uploadedFile" bean property,
 * invoking a "setUploadedFile" setter method.
 * <p>The type of the target property for a multipart file can be MultipartFile,
 * byte[], or String. The latter two receive the contents of the uploaded file;
 * all metadata like original file name, content type, etc are lost in those cases.
 * @param request the request with parameters to bind (can be multipart)
 * @see org.springframework.web.multipart.MultipartHttpServletRequest
 * @see org.springframework.web.multipart.MultipartFile
 * @see #bind(org.springframework.beans.PropertyValues)
 */
public void bind(ServletRequest request) {
    MutablePropertyValues mpvs = new ServletRequestParameterPropertyValues(request);
    MultipartRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartRequest.class);
    if (multipartRequest != null) {
        bindMultipart(multipartRequest.getMultiFileMap(), mpvs);
    } else if (StringUtils.startsWithIgnoreCase(request.getContentType(), MediaType.MULTIPART_FORM_DATA_VALUE)) {
        HttpServletRequest httpServletRequest = WebUtils.getNativeRequest(request, HttpServletRequest.class);
        if (httpServletRequest != null && HttpMethod.POST.matches(httpServletRequest.getMethod())) {
            StandardServletPartUtils.bindParts(httpServletRequest, mpvs, isBindEmptyMultipartFiles());
        }
    }
    addBindValues(mpvs, request);
    doBind(mpvs);
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) MultipartRequest(org.springframework.web.multipart.MultipartRequest)

Aggregations

MultipartRequest (org.springframework.web.multipart.MultipartRequest)6 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)2 MultipartFile (org.springframework.web.multipart.MultipartFile)2 MultipartHttpServletRequest (org.springframework.web.multipart.MultipartHttpServletRequest)2 DataType (com.revolsys.datatype.DataType)1 DataTypes (com.revolsys.datatype.DataTypes)1 Reader (com.revolsys.io.Reader)1 Writer (com.revolsys.io.Writer)1 Logs (com.revolsys.logging.Logs)1 RequestAttribute (com.revolsys.ui.web.annotation.RequestAttribute)1 HttpServletUtils (com.revolsys.ui.web.utils.HttpServletUtils)1 Exceptions (com.revolsys.util.Exceptions)1 Property (com.revolsys.util.Property)1 WrappedException (com.revolsys.util.WrappedException)1 Function3 (com.revolsys.util.function.Function3)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1