Search in sources :

Example 1 with FileParam

use of org.axe.bean.mvc.FileParam in project Axe by DongyuCai.

the class TestController method analysisParam.

public Data analysisParam(Param param) {
    Map<String, List<FormParam>> fieldMap = param.getFieldMap();
    Map<String, List<FileParam>> fileMap = param.getFileMap();
    Map<String, Object> model = new HashMap<>();
    //        fieldMap.entrySet().forEach(entry->model.put(entry.getKey(),entry.getValue()));
    for (Map.Entry<String, List<FileParam>> file : fileMap.entrySet()) {
        String fieldName = file.getKey();
        List<FileParam> fileParamList = file.getValue();
        List<String> fileNameList = new ArrayList<>();
        for (FileParam fileParam : fileParamList) {
            fileNameList.add(fileParam.getFileName());
        }
        model.put(fieldName, fileNameList);
    }
    Data data = new Data(model);
    return data;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Data(org.axe.bean.mvc.Data) FileParam(org.axe.bean.mvc.FileParam) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with FileParam

use of org.axe.bean.mvc.FileParam in project Axe by DongyuCai.

the class FormRequestHelper method createParam.

/**
     * 创建请求对象
     */
public static Param createParam(HttpServletRequest request, String requestPath, String mappingPath) {
    List<FormParam> formParamList = new ArrayList<>();
    List<FileParam> fileParamList = new ArrayList<>();
    try {
        //解析表单请求参数、文件
        Map<String, List<FileItem>> fileItemListMap = servletFileUpload.parseParameterMap(request);
        if (CollectionUtil.isNotEmpty(fileItemListMap)) {
            for (Map.Entry<String, List<FileItem>> fileItemListEntry : fileItemListMap.entrySet()) {
                String fieldName = fileItemListEntry.getKey();
                List<FileItem> fileItemList = fileItemListEntry.getValue();
                if (CollectionUtil.isNotEmpty(fileItemList)) {
                    for (FileItem fileItem : fileItemList) {
                        if (fileItem.isFormField()) {
                            String fieldValue = fileItem.getString("UTF-8");
                            formParamList.add(new FormParam(fieldName, fieldValue));
                        } else {
                            String fileName = FileUtil.getRealFileName(new String(fileItem.getName().getBytes(), "UTF-8"));
                            if (StringUtil.isNotEmpty(fileName)) {
                                long fileSize = fileItem.getSize();
                                String contentType = fileItem.getContentType();
                                InputStream inputStream = fileItem.getInputStream();
                                fileParamList.add(new FileParam(fieldName, fileName, fileSize, contentType, inputStream));
                            }
                        }
                    }
                }
            }
        }
        //解析url请求参数
        formParamList.addAll(RequestUtil.parseParameter(request, requestPath, mappingPath));
    } catch (Exception e) {
        LOGGER.error("create param failed", e);
        throw new RuntimeException(e);
    }
    return new Param(null, formParamList, fileParamList, null);
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) FileParam(org.axe.bean.mvc.FileParam) FileItem(org.apache.commons.fileupload.FileItem) FormParam(org.axe.bean.mvc.FormParam) Param(org.axe.bean.mvc.Param) FileParam(org.axe.bean.mvc.FileParam) ArrayList(java.util.ArrayList) List(java.util.List) FormParam(org.axe.bean.mvc.FormParam) Map(java.util.Map)

Example 3 with FileParam

use of org.axe.bean.mvc.FileParam in project Axe by DongyuCai.

the class FormRequestHelper method initParam.

/**
     * 创建请求对象
     */
public static void initParam(Param param, HttpServletRequest request, String requestPath, String mappingPath) {
    List<FormParam> formParamList = new ArrayList<>();
    List<FileParam> fileParamList = new ArrayList<>();
    try {
        //解析表单请求参数、文件
        Map<String, List<FileItem>> fileItemListMap = servletFileUpload.parseParameterMap(request);
        if (CollectionUtil.isNotEmpty(fileItemListMap)) {
            for (Map.Entry<String, List<FileItem>> fileItemListEntry : fileItemListMap.entrySet()) {
                String fieldName = fileItemListEntry.getKey();
                List<FileItem> fileItemList = fileItemListEntry.getValue();
                if (CollectionUtil.isNotEmpty(fileItemList)) {
                    for (FileItem fileItem : fileItemList) {
                        if (fileItem.isFormField()) {
                            String fieldValue = fileItem.getString("UTF-8");
                            formParamList.add(new FormParam(fieldName, fieldValue));
                        } else {
                            String fileName = FileUtil.getRealFileName(new String(fileItem.getName().getBytes(), "UTF-8"));
                            if (StringUtil.isNotEmpty(fileName)) {
                                long fileSize = fileItem.getSize();
                                String contentType = fileItem.getContentType();
                                InputStream inputStream = fileItem.getInputStream();
                                fileParamList.add(new FileParam(fieldName, fileName, fileSize, contentType, inputStream));
                            }
                        }
                    }
                }
            }
        }
        //解析url请求参数
        formParamList.addAll(RequestUtil.parseParameter(request, requestPath, mappingPath));
    } catch (Exception e) {
        LOGGER.error("create param failed", e);
        throw new RuntimeException(e);
    }
    param.init(null, formParamList, fileParamList, null);
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) FileParam(org.axe.bean.mvc.FileParam) FileItem(org.apache.commons.fileupload.FileItem) ArrayList(java.util.ArrayList) List(java.util.List) FormParam(org.axe.bean.mvc.FormParam) Map(java.util.Map)

Example 4 with FileParam

use of org.axe.bean.mvc.FileParam in project Axe by DongyuCai.

the class RequestUtil method getRequestParam.

public static Object getRequestParam(Param param, String fieldName, Type parameterType) {
    Object parameterValue = null;
    try {
        if (parameterType instanceof ParameterizedType) {
            //* 泛型,只支持List这样,但是不支持元素还是泛型了,比如List<Map<String,Object>这样就不支持
            Type[] actualTypes = ((ParameterizedType) parameterType).getActualTypeArguments();
            if (ReflectionUtil.compareType(List.class, (Class<?>) ((ParameterizedType) parameterType).getRawType())) {
                if (actualTypes.length > 0) {
                    Type listParamType = actualTypes[0];
                    if (listParamType instanceof WildcardType) {
                        //List<?>
                        parameterValue = RequestUtil.getParamList(fieldName, param);
                    } else {
                        List<FormParam> formParamList = param.getFieldMap().get(fieldName);
                        if (formParamList == null) {
                            formParamList = new ArrayList<>();
                        }
                        Class<?> listParamClass = (Class<?>) listParamType;
                        if (ReflectionUtil.compareType(Object.class, listParamClass)) {
                            //List<Object>
                            parameterValue = RequestUtil.getParamList(fieldName, param);
                        } else if (ReflectionUtil.compareType(String.class, listParamClass)) {
                            //List<String>
                            parameterValue = RequestUtil.getFormParamList(fieldName, param.getFieldMap());
                        } else if (ReflectionUtil.compareType(FileParam.class, listParamClass)) {
                            //List<FileParam>
                            parameterValue = RequestUtil.getFileParamList(fieldName, param.getFileMap());
                        } else if (ReflectionUtil.compareType(Byte.class, listParamClass)) {
                            //List<Byte>
                            List<Byte> list = new ArrayList<>();
                            for (int i = 0; i < formParamList.size(); i++) {
                                Byte value = CastUtil.castByte(formParamList.get(i).getFieldValue(), null);
                                list.add(value);
                            }
                            parameterValue = list.size() > 0 ? list : null;
                        } else if (ReflectionUtil.compareType(Boolean.class, listParamClass)) {
                            //List<Boolean>
                            List<Boolean> list = new ArrayList<>();
                            for (int i = 0; i < formParamList.size(); i++) {
                                Boolean value = CastUtil.castBoolean(formParamList.get(i), null);
                                list.add(value);
                            }
                            parameterValue = list.size() > 0 ? list : null;
                        } else if (ReflectionUtil.compareType(Short.class, listParamClass)) {
                            //List<Short>
                            List<Short> list = new ArrayList<>();
                            for (int i = 0; i < formParamList.size(); i++) {
                                Short value = CastUtil.castShort(formParamList.get(i).getFieldValue(), null);
                                list.add(value);
                            }
                            parameterValue = list.size() > 0 ? list : null;
                        } else if (ReflectionUtil.compareType(Character.class, listParamClass)) {
                            //List<Character>
                            List<Character> list = new ArrayList<>();
                            for (int i = 0; i < formParamList.size(); i++) {
                                Character value = CastUtil.castCharacter(formParamList.get(i).getFieldValue());
                                list.add(value);
                            }
                            parameterValue = list.size() > 0 ? list : null;
                        } else if (ReflectionUtil.compareType(Integer.class, listParamClass)) {
                            //List<Integer>
                            List<Integer> list = new ArrayList<>();
                            for (int i = 0; i < formParamList.size(); i++) {
                                Integer value = CastUtil.castInteger(formParamList.get(i).getFieldValue(), null);
                                list.add(value);
                            }
                            parameterValue = list.size() > 0 ? list : null;
                        } else if (ReflectionUtil.compareType(Long.class, listParamClass)) {
                            //List<Long>
                            List<Long> list = new ArrayList<>();
                            for (int i = 0; i < formParamList.size(); i++) {
                                Long value = CastUtil.castLong(formParamList.get(i).getFieldValue(), null);
                                list.add(value);
                            }
                            parameterValue = list.size() > 0 ? list : null;
                        } else if (ReflectionUtil.compareType(Float.class, listParamClass)) {
                            //List<Float>
                            List<Float> list = new ArrayList<>();
                            for (int i = 0; i < formParamList.size(); i++) {
                                Float value = CastUtil.castFloat(formParamList.get(i).getFieldValue(), null);
                                list.add(value);
                            }
                            parameterValue = list.size() > 0 ? list : null;
                        } else if (ReflectionUtil.compareType(Double.class, listParamClass)) {
                            //List<double>
                            List<Double> list = new ArrayList<>();
                            for (int i = 0; i < formParamList.size(); i++) {
                                Double value = CastUtil.castDouble(formParamList.get(i).getFieldValue(), null);
                                list.add(value);
                            }
                            parameterValue = list.size() > 0 ? list : null;
                        }
                    }
                }
            }
        } else if (parameterType instanceof Class) {
            Class<?> parameterClass = (Class<?>) parameterType;
            if (parameterClass.isArray()) {
                List<FormParam> formParamList = param.getFieldMap().get(fieldName);
                if (formParamList == null) {
                    formParamList = new ArrayList<>();
                }
                if (ReflectionUtil.compareType(parameterClass.getComponentType(), Object.class)) {
                    //Object
                    List<?> paramList = RequestUtil.getParamList(fieldName, param);
                    parameterValue = CollectionUtil.isNotEmpty(paramList) ? paramList.toArray() : null;
                } else if (ReflectionUtil.compareType(parameterClass.getComponentType(), String.class)) {
                    //String[]
                    List<String> paramList = RequestUtil.getFormParamList(fieldName, param.getFieldMap());
                    if (CollectionUtil.isNotEmpty(paramList)) {
                        String[] paramAry = new String[paramList.size()];
                        parameterValue = paramList.toArray(paramAry);
                    }
                } else if (ReflectionUtil.compareType(parameterClass.getComponentType(), FileParam.class)) {
                    //FileParam[]
                    List<FileParam> paramList = RequestUtil.getFileParamList(fieldName, param.getFileMap());
                    if (CollectionUtil.isNotEmpty(paramList)) {
                        FileParam[] paramAry = new FileParam[paramList.size()];
                        parameterValue = paramList.toArray(paramAry);
                    }
                } else if (ReflectionUtil.compareType(parameterClass.getComponentType(), Byte.class)) {
                    //Byte[]
                    Byte[] list = new Byte[formParamList.size()];
                    for (int i = 0; i < formParamList.size(); i++) {
                        Byte value = CastUtil.castByte(formParamList.get(i).getFieldValue(), null);
                        list[i] = value;
                    }
                    parameterValue = list.length > 0 ? list : null;
                } else if (ReflectionUtil.compareType(parameterClass.getComponentType(), Boolean.class)) {
                    //Boolean[]
                    Boolean[] list = new Boolean[formParamList.size()];
                    for (int i = 0; i < formParamList.size(); i++) {
                        Boolean value = CastUtil.castBoolean(formParamList.get(i), null);
                        list[i] = value;
                    }
                    parameterValue = list.length > 0 ? list : null;
                } else if (ReflectionUtil.compareType(parameterClass.getComponentType(), Short.class)) {
                    //Short[]
                    Short[] list = new Short[formParamList.size()];
                    for (int i = 0; i < formParamList.size(); i++) {
                        Short value = CastUtil.castShort(formParamList.get(i).getFieldValue(), null);
                        list[i] = value;
                    }
                    parameterValue = list.length > 0 ? list : null;
                } else if (ReflectionUtil.compareType(parameterClass.getComponentType(), Character.class)) {
                    //Character[]
                    Character[] list = new Character[formParamList.size()];
                    for (int i = 0; i < formParamList.size(); i++) {
                        Character value = CastUtil.castCharacter(formParamList.get(i).getFieldValue());
                        list[i] = value;
                    }
                    parameterValue = list.length > 0 ? list : null;
                } else if (ReflectionUtil.compareType(parameterClass.getComponentType(), Integer.class)) {
                    //Integer[]
                    Integer[] list = new Integer[formParamList.size()];
                    for (int i = 0; i < formParamList.size(); i++) {
                        Integer value = CastUtil.castInteger(formParamList.get(i).getFieldValue(), null);
                        list[i] = value;
                    }
                    parameterValue = list.length > 0 ? list : null;
                } else if (ReflectionUtil.compareType(parameterClass.getComponentType(), Long.class)) {
                    //Long[]
                    Long[] list = new Long[formParamList.size()];
                    for (int i = 0; i < formParamList.size(); i++) {
                        Long value = CastUtil.castLong(formParamList.get(i).getFieldValue(), null);
                        list[i] = value;
                    }
                    parameterValue = list.length > 0 ? list : null;
                } else if (ReflectionUtil.compareType(parameterClass.getComponentType(), Float.class)) {
                    //Float[]
                    Float[] list = new Float[formParamList.size()];
                    for (int i = 0; i < formParamList.size(); i++) {
                        Float value = CastUtil.castFloat(formParamList.get(i).getFieldValue(), null);
                        list[i] = value;
                    }
                    parameterValue = list.length > 0 ? list : null;
                } else if (ReflectionUtil.compareType(parameterClass.getComponentType(), Double.class)) {
                    //Double[]
                    Double[] list = new Double[formParamList.size()];
                    for (int i = 0; i < formParamList.size(); i++) {
                        Double value = CastUtil.castDouble(formParamList.get(i).getFieldValue(), null);
                        list[i] = value;
                    }
                    parameterValue = list.length > 0 ? list : null;
                }
            } else {
                List<FormParam> formParamList = param.getFieldMap().get(fieldName);
                if (formParamList == null) {
                    formParamList = new ArrayList<>();
                }
                if (ReflectionUtil.compareType(List.class, parameterClass)) {
                    //List
                    parameterValue = RequestUtil.getParamList(fieldName, param);
                } else if (ReflectionUtil.compareType(Object.class, parameterClass)) {
                    //Object
                    parameterValue = RequestUtil.getParamList(fieldName, param);
                } else if (ReflectionUtil.compareType(String.class, parameterClass)) {
                    //String
                    Map<String, List<FormParam>> fieldMap = param.getFieldMap();
                    if (fieldMap.containsKey(fieldName)) {
                        StringBuilder buffer = new StringBuilder("");
                        for (int j = 0; j < formParamList.size(); j++) {
                            String value = CastUtil.castString(formParamList.get(j).getFieldValue(), null);
                            if (value != null) {
                                buffer.append(value).append(",");
                            }
                        }
                        parameterValue = buffer.length() > 0 ? buffer.substring(0, buffer.length() - 1) : null;
                    }
                } else if (ReflectionUtil.compareType(parameterClass, FileParam.class)) {
                    //FileParam[]
                    List<FileParam> paramList = RequestUtil.getFileParamList(fieldName, param.getFileMap());
                    parameterValue = CollectionUtil.isNotEmpty(paramList) ? paramList.get(paramList.size() - 1) : null;
                } else if (ReflectionUtil.compareType(parameterClass, Byte.class)) {
                    //Byte
                    for (int i = 0; i < formParamList.size(); i++) {
                        Byte value = CastUtil.castByte(formParamList.get(i).getFieldValue(), null);
                        parameterValue = value;
                    }
                } else if (ReflectionUtil.compareType(parameterClass, Boolean.class)) {
                    //Boolean
                    for (int i = 0; i < formParamList.size(); i++) {
                        Boolean value = CastUtil.castBoolean(formParamList.get(i), null);
                        parameterValue = value;
                    }
                } else if (ReflectionUtil.compareType(parameterClass, Short.class)) {
                    //Short
                    for (int i = 0; i < formParamList.size(); i++) {
                        Short value = CastUtil.castShort(formParamList.get(i).getFieldValue(), null);
                        parameterValue = value;
                    }
                } else if (ReflectionUtil.compareType(parameterClass, Character.class)) {
                    //Character
                    for (int i = 0; i < formParamList.size(); i++) {
                        Character value = CastUtil.castCharacter(formParamList.get(i).getFieldValue());
                        parameterValue = value;
                    }
                } else if (ReflectionUtil.compareType(parameterClass, Integer.class)) {
                    //Integer
                    for (int i = 0; i < formParamList.size(); i++) {
                        Integer value = CastUtil.castInteger(formParamList.get(i).getFieldValue(), null);
                        parameterValue = value;
                    }
                } else if (ReflectionUtil.compareType(parameterClass, Long.class)) {
                    //Long
                    for (int i = 0; i < formParamList.size(); i++) {
                        Long value = CastUtil.castLong(formParamList.get(i).getFieldValue(), null);
                        parameterValue = value;
                    }
                } else if (ReflectionUtil.compareType(parameterClass, Float.class)) {
                    //Float
                    for (int i = 0; i < formParamList.size(); i++) {
                        Float value = CastUtil.castFloat(formParamList.get(i).getFieldValue(), null);
                        parameterValue = value;
                    }
                } else if (ReflectionUtil.compareType(parameterClass, Double.class)) {
                    //Double
                    for (int i = 0; i < formParamList.size(); i++) {
                        Double value = CastUtil.castDouble(formParamList.get(i).getFieldValue(), null);
                        parameterValue = value;
                    }
                }
            }
        }
    } catch (Exception e) {
        LOGGER.error("request analyze error", e);
    }
    return parameterValue;
}
Also used : ArrayList(java.util.ArrayList) ParameterizedType(java.lang.reflect.ParameterizedType) ArrayList(java.util.ArrayList) List(java.util.List) FileParam(org.axe.bean.mvc.FileParam) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) FormParam(org.axe.bean.mvc.FormParam) Map(java.util.Map)

Aggregations

ArrayList (java.util.ArrayList)4 List (java.util.List)4 Map (java.util.Map)4 FileParam (org.axe.bean.mvc.FileParam)4 FormParam (org.axe.bean.mvc.FormParam)3 BufferedInputStream (java.io.BufferedInputStream)2 InputStream (java.io.InputStream)2 FileItem (org.apache.commons.fileupload.FileItem)2 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 WildcardType (java.lang.reflect.WildcardType)1 HashMap (java.util.HashMap)1 Data (org.axe.bean.mvc.Data)1 Param (org.axe.bean.mvc.Param)1