struts2上传文件时,关于在action里面获取上传文件的文件名

来源:互联网 发布:东方卫视网络电视台 编辑:程序博客网 时间:2024/06/07 21:58

在struts2中有个文件上传的拦截器 FileUploadInterceptor 在它的intercept中我们可以找到答案。  

贴源码:

public String intercept(ActionInvocation invocation) throws Exception {        ActionContext ac = invocation.getInvocationContext();        HttpServletRequest request = (HttpServletRequest) ac.get(ServletActionContext.HTTP_REQUEST);        if (!(request instanceof MultiPartRequestWrapper)) {            if (LOG.isDebugEnabled()) {                ActionProxy proxy = invocation.getProxy();                LOG.debug(getTextMessage("struts.messages.bypass.request", new String[]{proxy.getNamespace(), proxy.getActionName()}));            }            return invocation.invoke();        }        ValidationAware validation = null;        Object action = invocation.getAction();        if (action instanceof ValidationAware) {            validation = (ValidationAware) action;        }        MultiPartRequestWrapper multiWrapper = (MultiPartRequestWrapper) request;        if (multiWrapper.hasErrors()) {            for (String error : multiWrapper.getErrors()) {                if (validation != null) {                    validation.addActionError(error);                }            }        }        // bind allowed Files        Enumeration fileParameterNames = multiWrapper.getFileParameterNames();        while (fileParameterNames != null && fileParameterNames.hasMoreElements()) {            // get the value of this input tag            String inputName = (String) fileParameterNames.nextElement();//获取页面 input 的name值            // get the content type            String[] contentType = multiWrapper.getContentTypes(inputName);//得到请求的所有类型            if (isNonEmpty(contentType)) {                // get the name of the file from the input tag                String[] fileName = multiWrapper.getFileNames(inputName);//得到请求中的所有文件名                 if (isNonEmpty(fileName)) {//如果不为空 继续                    // get a File object for the uploaded File                    File[] files = multiWrapper.getFiles(inputName);//通过文件名获取到所有文件                    if (files != null && files.length > 0) {//创建三个和上传文件数同等大小的list,分别表示文件集合、类型集合、文件名集合                        List<File> acceptedFiles = new ArrayList<File>(files.length);                        List<String> acceptedContentTypes = new ArrayList<String>(files.length);                        List<String> acceptedFileNames = new ArrayList<String>(files.length);                        String contentTypeName = inputName + "ContentType";                        String fileNameName = inputName + "FileName";//页面 input 的name值+"FileName" 就是存储文件名的变量名。例如       前台页面是:<input name="myfile" type="file" />。在action中我们就可以定义一个myfileFilename变量,并定义其get/set方法。  然后我们就可以直接使用myfileFilename得到上传文件的名称。        //下面的for循环就是 给前面创建的三个集合赋值,如果有的话for (int index = 0; index < files.length; index++) {if (acceptFile(action, files[index], fileName[index], contentType[index], inputName, validation)) {acceptedFiles.add(files[index]);acceptedContentTypes.add(contentType[index]); acceptedFileNames.add(fileName[index]);} } if (!acceptedFiles.isEmpty()) { Map<String, Object> params = ac.getParameters();//添加到parameters中 这样就可以通过OGNL注入到action了 params.put(inputName, acceptedFiles.toArray(new File[acceptedFiles.size()]));params.put(contentTypeName, acceptedContentTypes.toArray(new String[acceptedContentTypes.size()])); params.put(fileNameName, acceptedFileNames.toArray(new String[acceptedFileNames.size()]));} } }else{if (LOG.isWarnEnabled()) { LOG.warn(getTextMessage(action, "struts.messages.invalid.file", new String[]{inputName})); } } }else{if (LOG.isWarnEnabled()) {LOG.warn(getTextMessage(action, "struts.messages.invalid.content.type", new String[]{inputName})); } } } // invoke action return invocation.invoke(); }


 
阅读全文
0 0
原创粉丝点击