org.apache.commons.fileupload.MultipartStream Java Examples

The following examples show how to use org.apache.commons.fileupload.MultipartStream. 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: SpringManyMultipartFilesReader.java    From feign-form with Apache License 2.0 4 votes vote down vote up
@Override
protected MultipartFile[] readInternal (Class<? extends MultipartFile[]> clazz, HttpInputMessage inputMessage
) throws IOException {
  val headers = inputMessage.getHeaders();
  if (headers == null) {
    throw new HttpMessageNotReadableException("There are no headers at all.", inputMessage);
  }

  MediaType contentType = headers.getContentType();
  if (contentType == null) {
    throw new HttpMessageNotReadableException("Content-Type is missing.", inputMessage);
  }

  val boundaryBytes = getMultiPartBoundary(contentType);
  MultipartStream multipartStream = new MultipartStream(inputMessage.getBody(), boundaryBytes, bufSize, null);

  val multiparts = new LinkedList<ByteArrayMultipartFile>();
  for (boolean nextPart = multipartStream.skipPreamble(); nextPart; nextPart = multipartStream.readBoundary()) {
    ByteArrayMultipartFile multiPart;
    try {
      multiPart = readMultiPart(multipartStream);
    } catch (Exception e) {
      throw new HttpMessageNotReadableException("Multipart body could not be read.", e, inputMessage);
    }
    multiparts.add(multiPart);
  }
  return multiparts.toArray(new ByteArrayMultipartFile[0]);
}
 
Example #2
Source File: SpringManyMultipartFilesReader.java    From feign-form with Apache License 2.0 4 votes vote down vote up
private ByteArrayMultipartFile readMultiPart (MultipartStream multipartStream) throws IOException {
  val multiPartHeaders = splitIntoKeyValuePairs(
      multipartStream.readHeaders(),
      NEWLINES_PATTERN,
      COLON_PATTERN,
      false
  );

  val contentDisposition = splitIntoKeyValuePairs(
      multiPartHeaders.get(CONTENT_DISPOSITION),
      SEMICOLON_PATTERN,
      EQUALITY_SIGN_PATTERN,
      true
  );

  if (!contentDisposition.containsKey("form-data")) {
    throw new HttpMessageConversionException("Content-Disposition is not of type form-data.");
  }

  val bodyStream = new ByteArrayOutputStream();
  multipartStream.readBodyData(bodyStream);
  return new ByteArrayMultipartFile(
      contentDisposition.get("name"),
      contentDisposition.get("filename"),
      multiPartHeaders.get(CONTENT_TYPE),
      bodyStream.toByteArray()
  );
}