Example usage for org.apache.commons.fileupload FileItem getClass

List of usage examples for org.apache.commons.fileupload FileItem getClass

Introduction

In this page you can find the example usage for org.apache.commons.fileupload FileItem getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:se.unlogic.fileuploadutils.MultipartRequest.java

public MultipartRequest(int ramThreshold, long maxSize, Long maxFileSize, File repository,
        HttpServletRequest req) throws FileUploadException {

    this.originalRequest = req;

    factory = new SafeDiskFileItemFactory(ramThreshold, repository);

    uploadHandler = new ServletFileUpload(factory);
    uploadHandler.setSizeMax(maxSize);//from  w  ww  .ja v a 2s.  c o  m

    if (maxFileSize != null) {
        uploadHandler.setFileSizeMax(maxFileSize);
    }

    HashMap<String, List<String>> tempParameterMap = new HashMap<String, List<String>>();

    List<FileItem> items;
    try {
        items = uploadHandler.parseRequest(req);

        if (items != null && items.size() > 0) {
            for (FileItem item : items) {
                if (item.isFormField()) {

                    Field contentType = item.getClass().getDeclaredField("contentType");
                    contentType.setAccessible(true);
                    contentType.set(item, "charset=UTF-8;");

                    List<String> valueList = tempParameterMap.get(item.getFieldName());

                    if (valueList != null) {
                        valueList.add(item.getString());
                    } else {
                        valueList = new ArrayList<String>();
                        valueList.add(item.getString());
                        tempParameterMap.put(item.getFieldName(), valueList);
                    }

                } else {

                    List<FileItem> fileItems = fileMap.get(item.getFieldName());

                    if (fileItems == null) {

                        fileItems = new ArrayList<FileItem>(10);
                        fileMap.put(item.getFieldName(), fileItems);
                    }

                    fileItems.add(item);
                    this.fileItemList.add(item);
                }
            }
        }

        if (!tempParameterMap.isEmpty()) {

            for (Entry<String, List<String>> entry : tempParameterMap.entrySet()) {

                this.parameterMap.put(entry.getKey(),
                        entry.getValue().toArray(new String[entry.getValue().size()]));
            }
        }

    } catch (FileUploadException e) {
        factory.deleteFiles();
        throw e;
    } catch (IllegalAccessException e) {
        factory.deleteFiles();
    } catch (NoSuchFieldException e) {
        factory.deleteFiles();
    }
}