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 Object[]{proxy.getNamespace(), proxy.getActionName()}, ac.getLocale()));
}
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);
}
LOG.warn(error);
}
}
// bind allowed Files Enumeration fileParameterNames = multiWrapper.getFileParameterNames();
Map<String,List<Map<String,Object>>> fileParameterMap =
new HashMap<String,List<Map<String,Object>>>();
//文件值对 //zhaogy 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 File[] files = multiWrapper.getFiles(inputName);
if (files !=
null && files.length > 0) {
List<File> acceptedFiles =
new ArrayList<File>(files.length);
List<String> acceptedContentTypes =
new ArrayList<String>(files.length);
List<String> acceptedFileNames =
new ArrayList<String>(files.length);
List<String> renderNames =
new ArrayList<String>(files.length);
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, ac.getLocale())) {
acceptedFiles.add(files[index]);
acceptedContentTypes.add(contentType[index]);
acceptedFileNames.add(fileName[index]);
List<Map<String, Object>> vfl=
null;
if(fileParameterMap.containsKey(inputName)){
//是否已存在 vfl = fileParameterMap.get(inputName);
}
else{
vfl =
new ArrayList<Map<String,Object>>();
fileParameterMap.put(inputName, vfl);
}
Map<String, Object> value =
new HashMap<String,Object>();
value.put(
"contentType", contentType[index]);
value.put(
"fileName", fileName[index]);
value.put(
"acceptedFile", files[index]);
vfl.add(value);
}
}
if (!acceptedFiles.isEmpty()) {
Map<String, Object> params = ac.getParameters();
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()]));
params.put(
"fileParameterMap", fileParameterMap);
// zhaogy }
}
}
else {
LOG.warn(getTextMessage(action,
"struts.messages.invalid.file",
new Object[]{inputName}, ac.getLocale()));
}
}
else {
LOG.warn(getTextMessage(action,
"struts.messages.invalid.content.type",
new Object[]{inputName}, ac.getLocale()));
}
}
// invoke action String result = invocation.invoke();
// cleanup fileParameterNames = multiWrapper.getFileParameterNames();
while (fileParameterNames !=
null && fileParameterNames.hasMoreElements()) {
String inputValue = (String) fileParameterNames.nextElement();
File[] files = multiWrapper.getFiles(inputValue);
for (File currentFile : files) {
if (LOG.isInfoEnabled()) {
LOG.info(getTextMessage(action,
"struts.messages.removing.file",
new Object[]{inputValue, currentFile}, ac.getLocale()));
}
if ((currentFile !=
null) && currentFile.isFile()) {
if (currentFile.delete() ==
false) {
LOG.warn(
"Resource Leaking: Could not remove uploaded file '" + currentFile.getCanonicalPath() +
"'.");
}
}
}
}
return result;
}