class Cast exception >> Struts >> FormFile
private String resetFileList(FormFile currentFile, HashMap filesData){
Iterator it = filesData.keySet().iterator();
StringBuffer fileNames =new StringBuffer();
while (it.hasNext()){
FormFile tempFile = (FormFile) it.next();
fileNames.append(tempFile.getFileName()).append("\n");
}
return fileNames.toString();
}
I am using a html:file in my form. when the user submits the file for upload, in action I am storing the FormFile in to the session and reading adding the new file to the HashMap and setting the HashMap to the session again. This way I keep track of all FormFile objects and push them once, when the user submits the button.
I do not understand why I am getting class cast exception when I am reading the FormFile object from the HashMap i stored to the session.
Thanks in advance.
in your loop do this,
while (it.hasNext()) {
Object obj = it.next();
System.out.println(obj.getClass().getName());
FormFile tempFile = (FormFile) obj;
fileNames.append(tempFile.getFileName()).append("\n");
}
just noticed you are iterating over the keyset, not the values. Change that to get an iterator for the values
Iterator it = filesData.values().iterator();
No more editing!! I swear
Message was edited by:
SomeoneElse
yes. Hereunder is my new code.
private String resetFileList(FormFile currentFile, HashMap formFileMap) {
Iterator it = formFileMap.values().iterator();
StringBuffer fileNames = new StringBuffer();
while (it.hasNext()) {
Object obj=it.next();
System.out.println("class name: "+obj.getClass().getName());
FormFile formFile=(FormFile)obj;
System.out.println("class name: "+formFile.getClass().getName());
System.out.println("File name >>" + formFile.getFileName() + ", File Size>> " + formFile.getFileSize());
fileNames.append(formFile.getFileName() + "\n");
}
return fileNames.toString();
}
getting exception at the line where I casting to the FormFile.
Dear All
Thanks for all the postings.
I made a small mistake when I was setting the FormFile object. I made some changes after I posted the issue here. I was setting the bytes instead of the FormFile object. I corrected that all are working fine.
But I did what suggetsed by SomeOneElse to iterate against the values, instead of the keyset. That is the main problem I was getting classcast exception. Thanks to others who guided to debug the issue.
Cheers.. :)