SSH与SSM学习之SSH实现CRM练习12——文件上传原理
来源:互联网 发布:js获取标签的id 编辑:程序博客网 时间:2024/06/06 19:14
- SSH与SSM学习之SSH实现CRM练习12文件上传原理
- 一主要说明
- 二查看 struts-defaultxml
- 三FileUploadInterceptorjava
SSH与SSM学习之SSH实现CRM练习12——文件上传原理
一、主要说明
其实文件上传,主要就是通过 FileUploadInterceptor 这个拦截器,配合 ParametersInterceptor来完成的。
FileUploadInterceptor 把上传的文件 添加到值栈中,ParametersInterceptor 取出添加的值,赋值到对应的Action的属性中。
下面我们具体来看一下。
二、查看 struts-default.xml
我使用的是 struts 2.5
如下地址:
web\WEB-INF\lib\struts2-core-2.5.13.jar!\struts-default.xml
主要看到下面
..... <interceptors> ....... <interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/> <interceptor name="i18n" class="org.apache.struts2.interceptor.I18nInterceptor"/> <interceptor name="logger" class="com.opensymphony.xwork2.interceptor.LoggingInterceptor"/> <interceptor name="modelDriven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/> <interceptor name="scopedModelDriven" class="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor"/> <interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/> ....... <interceptor-stack name="defaultStack"> ....... <interceptor-ref name="fileUpload"/> <interceptor-ref name="checkbox"/> <interceptor-ref name="datetime"/> <interceptor-ref name="multiselect"/> <interceptor-ref name="staticParams"/> <interceptor-ref name="actionMappingParams"/> <interceptor-ref name="params"/> ....... </interceptor-stack> ....... </interceptors>.....
上面我们可以看到,默认的拦截器栈中,先调用 fileUpload,然后才是 params
三、FileUploadInterceptor.java
主要就是完成,获取文件、文件名、文件的ContentType 添加到值栈中,最后会调用到 ParametersInterceptor,赋值给Action中对应的属性。
public class FileUploadInterceptor extends AbstractInterceptor { public String intercept(ActionInvocation invocation) throws Exception { ....... // bind allowed Files Enumeration fileParameterNames = multiWrapper.getFileParameterNames(); while (fileParameterNames != null && fileParameterNames.hasMoreElements()) { // get the value of this input tag //获取文件的对应的属性名 String inputName = (String) fileParameterNames.nextElement(); // 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 UploadedFile[] files = multiWrapper.getFiles(inputName); if (files != null && files.length > 0) { List<UploadedFile> acceptedFiles = new ArrayList<>(files.length); List<String> acceptedContentTypes = new ArrayList<>(files.length); List<String> acceptedFileNames = new ArrayList<>(files.length); //拼接文件的ContentType属性名 String contentTypeName = inputName + "ContentType"; //拼接文件名的属性名 String fileNameName = inputName + "FileName"; 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, Parameter> newParams = new HashMap<>(); //把文件添加到值栈中 newParams.put(inputName, new Parameter.File(inputName, acceptedFiles.toArray(new UploadedFile[acceptedFiles.size()]))); //把文件的ContentType添加到值栈中 newParams.put(contentTypeName, new Parameter.File(contentTypeName, acceptedContentTypes.toArray(new String[acceptedContentTypes.size()]))); //把文件名添加到值栈中 newParams.put(fileNameName, new Parameter.File(fileNameName, acceptedFileNames.toArray(new String[acceptedFileNames.size()]))); ac.getParameters().appendAll(newParams); } } } 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})); } } } return invocation.invoke(); }}
阅读全文
0 0
- SSH与SSM学习之SSH实现CRM练习12——文件上传原理
- SSH与SSM学习之SSH实现CRM练习11——文件上传
- SSH与SSM学习之SSH实现CRM练习02——客户列表02_PageBean
- SSH与SSM学习之SSH实现CRM练习03——客户列表03_CustomerDao
- SSH与SSM学习之SSH实现CRM练习04——客户列表04_CustomerService
- SSH与SSM学习之SSH实现CRM练习05——客户列表05_CustomerAction
- SSH与SSM学习之SSH实现CRM练习07——BaseDao封装
- SSH与SSM学习之SSH实现CRM练习08——数据字典表
- SSH与SSM学习之SSH实现CRM练习09——新增客户
- SSH与SSM学习之SSH实现CRM练习13——添加联系人
- SSH与SSM学习之SSH实现CRM练习14——联系人列表
- SSH与SSM学习之SSH实现CRM练习15——注册登录
- SSH与SSM学习之SSH实现CRM练习16——登录校验拦截器
- SSH与SSM学习之SSH实现CRM练习01——客户列表01_客户列表分析
- SSH与SSM学习之SSH实现CRM练习06——客户列表06_页面和测试
- SSH与SSM学习之SSH实现CRM练习10——编辑客户和删除客户
- SSH与SSM学习之SSH实现CRM练习17——添加客户拜访记录和列表
- SSH与SSM学习之SSH整合01——SSH整合原理和导包
- 做AI必须要知道的十种深度学习方法
- Linux基础之常用命令
- 机器学习小白如何成长为业内专家?
- 单例模式
- Set集合
- SSH与SSM学习之SSH实现CRM练习12——文件上传原理
- Content-disposition
- rxjava 和retrofit 结合使用
- HDFS NamenNode详解
- 机器学习实战_kNN算法python3.6实现与理解
- 工作分配问题
- C++ 成员变量为引用类型和const类型,如何赋值
- 老毛桃winpe u盘版版告别繁琐,简单易用,一盘两用,携带方便.不需要任何技术基础,一键制作,自动完成制作,平时当U盘使用,需要的时候就是修复盘,完全不需要光驱和光盘,携带方便。电脑应急,工具齐全,
- [学习笔记]markdown初体验