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
- struts2上传文件时,关于在action里面获取上传文件的文件名
- struts2上传文件的时候,action里面获取上传文件的文件名和类型的原理是什么?
- struts2上传文件的时候,action里面获取上传文件的文件名和类型的原理是什么?
- action中如何获取上传文件的文件名和类型
- 获取上传文件的文件名~~
- 获取上传文件的文件名
- struts2文件上传获取文件名、文件类型
- struts2如何获取上传文件的文件名和文件类型
- 关于在struts2的文件简单上传
- Extjs关于获取上传文件的格式和文件名
- 在S2SH中,struts2文件上传的action
- struts2的action并发文件上传
- 上传文件,并获取文件名
- struts2文件上传(Action方法)
- struts2 文件上传时,某次上传失败之后的所有上传都返回input,不跳到action
- struts2 文件上传时,某次上传失败之后的所有上传都返回input,不跳到action
- 关于struts2中文件上传获取不到文件名的问题
- 在文件上传时取完整的文件名
- 字符串替换:用参数字符数组成员替换字符串中的占位符(面试题)
- 堆排序
- Eclipse怎样连接并打开oracle等数据库?
- select编程
- class path resource cannot be opened because it does not exits
- struts2上传文件时,关于在action里面获取上传文件的文件名
- Windows下的Python安装pip,及使用技巧
- linux-桥接与网卡聚合
- 动态规划解N个矩阵链乘问题
- Eclipse 运行配置(Run Configuration)
- android studio 出现 failed to find target android- 错误解决的办法
- Java交换排序
- 金币(NOIP2015)
- POJ 2411 Mondriaan's Dream