Search in sources :

Example 1 with FileSizeExceededException

use of cn.taketoday.http.FileSizeExceededException in project today-infrastructure by TAKETODAY.

the class AbstractMultipartResolver method resolveInternal.

/**
 * @throws FileSizeExceededException upload file size exceeded
 * @see MultipartConfig#getMaxRequestSize()
 */
@Override
protected Object resolveInternal(RequestContext context, ResolvableMethodParameter parameter) throws Throwable {
    if (context.isMultipart()) {
        DataSize maxRequestSize = getMultipartConfig().getMaxRequestSize();
        // exceed max size?
        if (maxRequestSize.toBytes() < context.getContentLength()) {
            throw new FileSizeExceededException(maxRequestSize, null, DataSize.ofBytes(context.getContentLength()));
        }
        MultiValueMap<String, MultipartFile> multipartFiles = context.getMultipartRequest().getMultipartFiles();
        return resolveInternal(context, parameter, multipartFiles);
    }
    return null;
}
Also used : MultipartFile(cn.taketoday.web.multipart.MultipartFile) DataSize(cn.taketoday.util.DataSize) FileSizeExceededException(cn.taketoday.http.FileSizeExceededException)

Example 2 with FileSizeExceededException

use of cn.taketoday.http.FileSizeExceededException in project today-framework by TAKETODAY.

the class LightRequestContext method getRequestParts.

/**
 * @throws FileSizeExceededException
 */
private List<RequestPart> getRequestParts() {
    if (requestParts == null) {
        try {
            final long contentLength = getContentLength();
            final LightHttpConfig config = this.config;
            final MultipartConfiguration multipartConfig = config.getMultipartConfig();
            if (contentLength > multipartConfig.getMaxRequestSize().toBytes()) {
                throw new FileSizeExceededException(multipartConfig.getMaxRequestSize(), null).setActual(DataSize.ofBytes(contentLength));
            }
            final MultipartIterator multipartIterator = new MultipartIterator(request);
            final MultipartInputStream inputStream = multipartIterator.getInputStream();
            final ArrayList<RequestPart> parts = new ArrayList<>();
            while (multipartIterator.hasNext(inputStream)) {
                parts.add(multipartIterator.obtainNext(config, multipartConfig));
            }
            requestParts = parts;
        } catch (IOException e) {
            throw new MultipartException("multipart read failed", e);
        }
    }
    return requestParts;
}
Also used : MultipartConfiguration(cn.taketoday.web.multipart.MultipartConfiguration) MultipartException(cn.taketoday.web.bind.MultipartException) FileSizeExceededException(cn.taketoday.http.FileSizeExceededException) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 3 with FileSizeExceededException

use of cn.taketoday.http.FileSizeExceededException in project today-framework by TAKETODAY.

the class MultipartIterator method obtainNext.

/**
 * @throws NotMultipartRequestException if this request is not of type multipart/form-data
 * @throws MultipartException multipart parse failed
 */
public RequestPart obtainNext(LightHttpConfig config, MultipartConfiguration multipartConfig) throws IOException {
    hasNext = false;
    // 先解析 header
    // Content-Disposition
    final String contentDispositionString = Utils.readLine(inputStream);
    // Content-Type
    final String contentType = Utils.readLine(inputStream);
    // final HttpHeaders httpHeaders = Utils.readHeaders(inputStream, config);
    final MultipartInputStream inputStream = this.inputStream;
    final int partSize = inputStream.tail - inputStream.head;
    final DataSize maxFileSize = multipartConfig.getMaxFileSize();
    if (partSize > maxFileSize.toBytes()) {
        throw new FileSizeExceededException(maxFileSize, null).setActual(DataSize.ofBytes(partSize));
    }
    final ContentDisposition contentDisposition = ContentDisposition.parse(contentDispositionString);
    // if (httpHeaders.containsKey(Constant.CONTENT_TYPE)) {
    if (StringUtils.isNotEmpty(contentType)) {
        if (partSize > config.getMaxMultipartInMemSize()) {
            final String tempLocation = multipartConfig.getLocation();
            final File tempFileDir = new File(tempLocation);
            final String randomString = StringUtils.generateRandomString(10);
            final File tempFile = new File(tempFileDir, randomString);
            // save to temp file
            try (final FileOutputStream fileOutput = new FileOutputStream(tempFile)) {
                final int bufferSize = config.getMultipartBufferSize();
                // readTimes > 1
                final int readTimes = partSize / bufferSize;
                if (readTimes == 0) {
                    // part size 太小了 直接一次性读完
                    byte[] buffer = Utils.readBytes(inputStream, partSize);
                    fileOutput.write(buffer, 0, partSize);
                    final LightMultipartFile multipartFile = new LightMultipartFile(tempFile, contentDisposition, contentType, partSize);
                    multipartFile.setCachedBytes(buffer);
                    return multipartFile;
                } else {
                    // 分次读取
                    byte[] buffer = new byte[bufferSize];
                    int bytesRead = 0;
                    for (int i = 0; i < readTimes; i++) {
                        bytesRead += inputStream.read(buffer, 0, bufferSize);
                        fileOutput.write(buffer, 0, bytesRead);
                    }
                    // 读取剩余字节
                    buffer = Utils.readBytes(inputStream, partSize - bytesRead);
                    fileOutput.write(buffer);
                    return new LightMultipartFile(tempFile, contentDisposition, contentType, partSize);
                }
            }
        } else {
            final byte[] bytes = Utils.readBytes(inputStream, partSize);
            // inputStream memory
            return new LightMultipartFile(bytes, contentDisposition, contentType, partSize);
        }
    } else {
        final String name = contentDisposition.getName();
        final byte[] bytes = Utils.readBytes(inputStream, partSize);
        return new FieldRequestPart(bytes, name);
    }
}
Also used : ContentDisposition(cn.taketoday.http.ContentDisposition) DataSize(cn.taketoday.util.DataSize) FileOutputStream(java.io.FileOutputStream) FileSizeExceededException(cn.taketoday.http.FileSizeExceededException) File(java.io.File)

Aggregations

FileSizeExceededException (cn.taketoday.http.FileSizeExceededException)3 DataSize (cn.taketoday.util.DataSize)2 ContentDisposition (cn.taketoday.http.ContentDisposition)1 MultipartException (cn.taketoday.web.bind.MultipartException)1 MultipartConfiguration (cn.taketoday.web.multipart.MultipartConfiguration)1 MultipartFile (cn.taketoday.web.multipart.MultipartFile)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1