Search in sources :

Example 1 with MaxUploadSizeExceededException

use of cn.taketoday.web.multipart.MaxUploadSizeExceededException in project today-framework by TAKETODAY.

the class ServletMultipartRequest method parseRequest.

private MultiValueMap<String, MultipartFile> parseRequest(HttpServletRequest request) {
    try {
        Collection<Part> parts = request.getParts();
        LinkedMultiValueMap<String, MultipartFile> files = new LinkedMultiValueMap<>(parts.size());
        for (Part part : parts) {
            String headerValue = part.getHeader(HttpHeaders.CONTENT_DISPOSITION);
            ContentDisposition disposition = ContentDisposition.parse(headerValue);
            String filename = disposition.getFilename();
            files.add(part.getName(), new ServletPartMultipartFile(part, filename));
        }
        return files;
    } catch (IOException e) {
        throw new MultipartException("MultipartFile parsing failed.", e);
    } catch (ServletException e) {
        throw new NotMultipartRequestException("This is not a multipart request", e);
    } catch (Throwable ex) {
        String msg = ex.getMessage();
        if (msg != null && msg.contains("size") && msg.contains("exceed")) {
            throw new MaxUploadSizeExceededException(-1, ex);
        }
        throw new MultipartException("Failed to parse multipart servlet request", ex);
    }
}
Also used : LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) IOException(java.io.IOException) ServletException(jakarta.servlet.ServletException) NotMultipartRequestException(cn.taketoday.web.bind.NotMultipartRequestException) MultipartFile(cn.taketoday.web.multipart.MultipartFile) MaxUploadSizeExceededException(cn.taketoday.web.multipart.MaxUploadSizeExceededException) ContentDisposition(cn.taketoday.http.ContentDisposition) Part(jakarta.servlet.http.Part) MultipartException(cn.taketoday.web.bind.MultipartException)

Example 2 with MaxUploadSizeExceededException

use of cn.taketoday.web.multipart.MaxUploadSizeExceededException in project today-infrastructure by TAKETODAY.

the class ServletMultipartRequest method parseRequest.

private MultiValueMap<String, MultipartFile> parseRequest(HttpServletRequest request) {
    try {
        Collection<Part> parts = request.getParts();
        LinkedMultiValueMap<String, MultipartFile> files = new LinkedMultiValueMap<>(parts.size());
        for (Part part : parts) {
            String headerValue = part.getHeader(HttpHeaders.CONTENT_DISPOSITION);
            ContentDisposition disposition = ContentDisposition.parse(headerValue);
            String filename = disposition.getFilename();
            if (filename != null) {
                if (filename.startsWith("=?") && filename.endsWith("?=")) {
                    filename = MimeDelegate.decode(filename);
                }
                files.add(part.getName(), new ServletPartMultipartFile(part, filename));
            }
        }
        return files;
    } catch (IOException e) {
        throw new MultipartException("MultipartFile parsing failed.", e);
    } catch (ServletException e) {
        throw new NotMultipartRequestException("This is not a multipart request", e);
    } catch (Throwable ex) {
        String msg = ex.getMessage();
        if (msg != null && msg.contains("size") && msg.contains("exceed")) {
            throw new MaxUploadSizeExceededException(-1, ex);
        }
        throw new MultipartException("Failed to parse multipart servlet request", ex);
    }
}
Also used : LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) IOException(java.io.IOException) ServletException(jakarta.servlet.ServletException) NotMultipartRequestException(cn.taketoday.web.bind.NotMultipartRequestException) ServletPartMultipartFile(cn.taketoday.web.multipart.ServletPartMultipartFile) MultipartFile(cn.taketoday.web.multipart.MultipartFile) MaxUploadSizeExceededException(cn.taketoday.web.multipart.MaxUploadSizeExceededException) ServletPartMultipartFile(cn.taketoday.web.multipart.ServletPartMultipartFile) ContentDisposition(cn.taketoday.http.ContentDisposition) Part(jakarta.servlet.http.Part) MultipartException(cn.taketoday.web.bind.MultipartException)

Aggregations

LinkedMultiValueMap (cn.taketoday.core.LinkedMultiValueMap)2 ContentDisposition (cn.taketoday.http.ContentDisposition)2 MultipartException (cn.taketoday.web.bind.MultipartException)2 NotMultipartRequestException (cn.taketoday.web.bind.NotMultipartRequestException)2 MaxUploadSizeExceededException (cn.taketoday.web.multipart.MaxUploadSizeExceededException)2 MultipartFile (cn.taketoday.web.multipart.MultipartFile)2 ServletException (jakarta.servlet.ServletException)2 Part (jakarta.servlet.http.Part)2 IOException (java.io.IOException)2 ServletPartMultipartFile (cn.taketoday.web.multipart.ServletPartMultipartFile)1