All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mars.netty.util.FileItemUtil Maven / Gradle / Ivy

The newest version!
package com.mars.netty.util;

import com.mars.server.server.request.HttpMarsRequest;
import com.mars.server.server.request.model.MarsFileUpLoad;
import org.apache.tomcat.util.http.fileupload.FileItem;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 如果是文件上传的提交,处理表单字段和文件流
 */
public class FileItemUtil {

    /**
     * 如果是文件上传的提交,处理表单字段和文件流
     * @param fileItemList 文件列表
     * @param marsRequest mars请求
     * @return 加工后的mars请求
     * @throws Exception 异常
     */
    public static HttpMarsRequest getHttpMarsRequest(List fileItemList, HttpMarsRequest marsRequest) throws Exception{
        if(fileItemList != null){
            Map files = new HashMap<>();
            Map> marsParams = new HashMap<>();
            for(FileItem item : fileItemList){
                if(item.isFormField()){
                    String name = item.getFieldName();
                    String value = item.getString("UTF-8");
                    List params = marsParams.get(name);
                    if(params == null){
                        params = new ArrayList<>();
                    }
                    params.add(value);
                    marsParams.put(name,params);
                } else {
                    MarsFileUpLoad marsFileUpLoad = new MarsFileUpLoad();
                    marsFileUpLoad.setName(item.getFieldName());
                    marsFileUpLoad.setInputStream(item.getInputStream());
                    marsFileUpLoad.setFileName(item.getName());
                    files.put(marsFileUpLoad.getName(),marsFileUpLoad);
                }
            }
            marsRequest.setFiles(files);
            marsRequest.setParams(marsParams);
        }
        return marsRequest;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy