日期:2014-05-20  浏览次数:20919 次

java.util.ArrayList cannot be cast to org.apache.struts.upload.FormFile
请问这个问题怎么解决?
我在Action里面,程序经过:List<UploadFile> uploadFileList = super.uploadFile(form, true, 0, new int[] { 120, 240, 400, 640 });
这句话的时候,就报上面的错误;
错误提示:
java.lang.ClassCastException: java.util.ArrayList cannot be cast to org.apache.struts.upload.FormFile
com.ebiz.ssi.web.struts.BaseSsiAction.uploadFile(BaseSsiAction.java:255)
com.ebiz.ssi.web.struts.BaseSsiAction.uploadFile(BaseSsiAction.java:203)
com.ebiz.easybiz.web.struts.admin.manager.NewsInfoAllAction.save(NewsInfoAllAction.java:229)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:269)


其中BaseSsiAction.java中该方法代码如下:
protected transient List uploadFile(ActionForm form, String uploadDir, boolean isResizeImage, int resizeType, int resizeVersions[])
throws Exception
{
if (StringUtils.isBlank(uploadDir))
uploadDir = StringUtils.join(new String[] {
"files", "upload", ""
}, File.separator);
String folderPatterns[] = {
"yyyy", "MM", "dd", ""
};
String autoCreatedDateDir = DateFormatUtils.format(new Date(), StringUtils.join(folderPatterns, File.separator));
String ctxDir = getServlet().getServletContext().getRealPath(File.separator);
if (!ctxDir.endsWith(File.separator))
ctxDir = (new StringBuilder(String.valueOf(ctxDir))).append(File.separator).toString();
File savePath = new File((new StringBuilder(String.valueOf(ctxDir))).append(uploadDir).append(autoCreatedDateDir).toString());
logger.debug("===> save path is: {}", savePath);
if (!savePath.exists())
savePath.mkdirs();
List uploadFileList = new ArrayList();
UploadFile uploadFile = null;
int i = 0;
Hashtable fileh = form.getMultipartRequestHandler().getFileElements();
for (Enumeration e = fileh.keys(); e.hasMoreElements();)
{
String key = (String)e.nextElement();
FormFile formFile = (FormFile)fileh.get(key);
String fileName = formFile.getFileName().trim();
if (!"".equals(fileName))
{
uploadFile = new UploadFile();
uploadFile.setContentType(formFile.getContentType());
uploadFile.setFileSize(formFile.getFileSize());
uploadFile.setFileName(formFile.getFileName().trim());
uploadFile.setFormName(key);
String fileSaveName = StringUtils.join(new String[] {
UUID.randomUUID().toString(), ".", uploadFile.getExtension()
});
String fileSavePath = (new StringBuilder(String.valueOf(uploadDir))).append(autoCreatedDateDir).append(fileSaveName).toString();
uploadFile.setFileSaveName(fileSaveName);
uploadFile.setFileSavePath(StringUtils.replace(fileSavePath, File.separator, "/"));
logger.debug(uploadFile.toString());
uploadFileList.add(uploadFile);
InputStream ins = formFile.getInputStream();
OutputStream os = new FileOutputStream((new StringBuilder(String.valueOf(ctxDir))).append(fileSavePath).toString());
int bytesRead = 0;
byte buffer[] = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) //代码中第255行
os.write(buffer, 0, bytesRead);
os.close();
ins.close();

return uploadFileList;
}

请教高手啊 !

------解决方案--------------------
类型转换问题