struts 2アップロードされたファイルの処理
3421 ワード
public abstract class ActionEngine extends ActionSupport implements ServletRequestAware
{
private HttpServletRequest httpServletRequest;
public void setServletRequest(HttpServletRequest servletRequest)
{
this.httpServletRequest = servletRequest;
}
protected Map<String, Object> fileMap = new HashMap<String, Object>();
public void preExecute() throws Exception
{
if (httpServletRequest instanceof MultiPartRequestWrapper)
{
MultiPartRequestWrapper requestWrapper = (MultiPartRequestWrapper) httpServletRequest;
if (requestWrapper.hasErrors())
{
Collection<String> errors = requestWrapper.getErrors();
if (errors != null && !errors.isEmpty())
{
Iterator iterator = errors.iterator();
long maxSize = 0;
if (requestWrapper.getAttribute("maxSize") != null)
{
maxSize = Long.parseLong(requestWrapper.getAttribute("maxSize")
.toString());
maxSize = maxSize / 1024 / 1024;
}
while (iterator.hasNext())
{
throw new XXXException;
}
}
return;
}
Enumeration<String> parameterNames = requestWrapper.getFileParameterNames();
while (parameterNames.hasMoreElements())
{
@SuppressWarnings("unused")
String paramterName = parameterNames.nextElement();
File[] files = requestWrapper.getFiles(paramterName);
if (null == files || files.length == 0)
{
continue;
}
else if (files.length == 1)
{
FileBean fileBean = new FileBean();
fileBean.setAbsolutePath(files[0].getAbsolutePath());
fileBean.setFileSize(files[0].length());
fileBean.setFilename(requestWrapper.getFileNames(paramterName)[0]);
this.fileMap.put(paramterName, fileBean);
}
else
{
ArrayList<FileBean> list = new ArrayList();
for (int i = 0; i < files.length; i++)
{
File file = files[i];
FileBean fileBean = new FileBean();
fileBean.setAbsolutePath(file.getAbsolutePath());
fileBean.setFileSize(file.length());
fileBean.setFilename(requestWrapper.getFileNames(paramterName)[i]);
list.add(fileBean);
}
this.fileMap.put(paramterName, list);
}
}
}
}
}