MultipartUtils { public static boolean isFormField(final Map<String, List<String>> headers, final MultipartContext context){ final boolean hasFileName = getFileName(headers) != null; if (hasFileName){ return false; } final boolean hasFieldName = getFieldName(headers) != null; if (!hasFieldName){ return false; } final String contentType = getContentType(headers); final boolean isMultipartFormData = context.getContentType().startsWith(MULTIPART_FORM_DATA); if (!isMultipartFormData){ return false; } final boolean isTextPlain = contentType == null || contentType.startsWith(TEXT_PLAIN); if (!isTextPlain){ return false; } return true; } private  MultipartUtils(); static boolean isMultipart(final String contentTypeHeaderValue); static boolean hasMultipartContentType(final Map<String, List<String>> headers); static String getContentType(final Map<String, List<String>> headers); static long getContentLength(final Map<String, List<String>> headers); static String getCharEncoding(final Map<String, List<String>> headers); static List<String> getHeaders(final String headerName, final Map<String, List<String>> headers); static String getHeader(final String headerName, final Map<String, List<String>> headers); static boolean isFormField(final Map<String, List<String>> headers, final MultipartContext context); static String readFormParameterValue(final StreamStorage streamStorage, final Map<String, List<String>> headers); static String getFileName(final Map<String, List<String>> headers); static String getFieldName(final Map<String, List<String>> headers); static boolean isContentTransferEncodingBase64Encoded(final Map<String, List<String>> partHeaders); static byte[] getBoundary(final String contentType); static final byte DASH; static final byte CR; static final byte LF; static final byte[] HEADER_DELIMITER; static final String MULTIPART; static final String CONTENT_DISPOSITION; static final String CONTENT_TRANSFER_ENCODING; static final String CONTENT_LENGTH; static final String CONTENT_TYPE; static final String FORM_DATA; static final String ATTACHMENT; static final String MULTIPART_FORM_DATA; static final String MULTIPART_MIXED; static final String TEXT_PLAIN; }
@Test public void testIsFormField(){ MultipartContext multipartContext = new MultipartContext("multipart/form-data; boundary=--------1234", 1045, "UTF-8"); assertFalse(MultipartUtils.isFormField( new HashMap<String, List<String>>(){{ put(MultipartUtils.CONTENT_TYPE.toLowerCase(), Collections.singletonList("text/plain")); put(MultipartUtils.CONTENT_DISPOSITION.toLowerCase(), Collections.singletonList("form-data; name=\"file\"; filename=\"file.txt\"")); }}, multipartContext)); assertFalse(MultipartUtils.isFormField( new HashMap<String, List<String>>(){{ put(MultipartUtils.CONTENT_TYPE.toLowerCase(), Collections.singletonList("text/plain")); put(MultipartUtils.CONTENT_DISPOSITION.toLowerCase(), Collections.singletonList("form-data; filename=\"file.txt\"")); }}, multipartContext)); assertTrue(MultipartUtils.isFormField( new HashMap<String, List<String>>(){{ put(MultipartUtils.CONTENT_TYPE.toLowerCase(), Collections.singletonList("text/plain")); put(MultipartUtils.CONTENT_DISPOSITION.toLowerCase(), Collections.singletonList("form-data; name=\"file\"")); }} , multipartContext)); assertFalse(MultipartUtils.isFormField( new HashMap<String, List<String>>(){{ put(MultipartUtils.CONTENT_TYPE.toLowerCase(), Collections.singletonList("application/json")); put(MultipartUtils.CONTENT_DISPOSITION.toLowerCase(), Collections.singletonList("form-data; name=\"file\"")); }}, multipartContext)); assertFalse(MultipartUtils.isFormField( new HashMap<String, List<String>>(){{ put(MultipartUtils.CONTENT_TYPE.toLowerCase(), Collections.singletonList("text/plain")); put(MultipartUtils.CONTENT_DISPOSITION.toLowerCase(), Collections.singletonList("form-data")); }}, multipartContext)); assertFalse(MultipartUtils.isFormField( new HashMap<String, List<String>>(){{ put(MultipartUtils.CONTENT_TYPE.toLowerCase(), Collections.singletonList("text/plain")); put(MultipartUtils.CONTENT_DISPOSITION.toLowerCase(), Collections.singletonList("form-data; name=\"file\"")); }}, new MultipartContext("multipart/mixed; boundary=--------1234", 1045, "UTF-8"))); }