struts2上传文件时,关于在action里面获取上传文件的文件名
2017-08-12 23:24
393 查看
在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 + "F
4000
ileName";//页面 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();
}
贴源码:
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 + "F
4000
ileName";//页面 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();
}
相关文章推荐
- struts2上传文件的时候,action里面获取上传文件的文件名和类型的原理是什么?
- struts2上传文件的时候,action里面获取上传文件的文件名和类型的原理是什么?
- 关于struts2中文件上传获取不到文件名的问题
- 关于Struts2中文件上传代码ServletActionContext.getServletContext()编译报错的问题,
- action中如何获取上传文件的文件名和类型
- 关于Struts2 上传文件获取文件名称和类型(标准做法)
- 关于struts2上传文件报错“No result defined for action and result input”
- struts2如何获取上传文件的文件名和文件类型
- 关于struts2上传文件报错“No result defined for action and result input”
- struts2文件上传获取文件名、文件类型
- Struts2中文件上传和多文件上传以及获取HttpServletRequest / HttpSession / ServletContext / HttpServletRespons
- struts2 上传文件在action 里的属性 name
- 关于struts2文件上传中的uploadContentType和uploadFileName
- 4.3.3: Struts2文件上传---配置文件上传的Action
- 关于在struts2的文件简单上传
- Struts2文件上传无法取得文件名及文件类型问题的解决
- 关于js获取上传的文件信息
- 关于Struts2文件上传下载功能整合信息(1)
- struts2 上传文件得到的是.tmp 如何得到原有文件名
- 关于Struts2的多文件上传