Please add Multipart Resolver in your spring config (app config) like below:
<bean id="multiPartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
سؤال
I am trying to upload Excel file and print the content of each cell in console. Here is my code ..
JSP :
<c:url value="/my-account/readExcel" var="readExcel" />
<form:form action="${readExcel}" method="post" commandName="excelFileUploadForm" enctype="multipart/form-data">
<form:input id="fineName" path="fileName" type="file" />
<input type="submit" value="Uplaod" />
</form:form>
ExcelFileUploadForm:
public class ExcelFileUploadForm
{
private MultipartFile fileName;
public MultipartFile getFileName()
{
return fileName;
}
public void setFileName(final MultipartFile fileName)
{
this.fileName = fileName;
}
}
Controller :
@RequestMapping(value = "/readExcel", method = RequestMethod.POST)
@RequireHardLogIn
public String readExcel(final HttpServletRequest request, final HttpServletResponse response) throws CMSItemNotFoundException,
IOException
{
final MultipartHttpServletRequest mpr = (MultipartHttpServletRequest) request;
final CommonsMultipartFile file = (CommonsMultipartFile) mpr.getFile("fileName");
read(file);
return "redirect:/my-account";
}
public void read(final CommonsMultipartFile inputFile) throws IOException
{
Workbook w;
try
{
w = Workbook.getWorkbook(inputFile.getInputStream());
// Get the first sheet
final Sheet sheet = w.getSheet(0);
// Loop over first 10 column and lines
for (int j = 0; j < sheet.getColumns(); j++)
{
for (int i = 0; i < sheet.getRows(); i++)
{
final Cell cell = sheet.getCell(j, i);
final CellType type = cell.getType();
if (type == CellType.LABEL)
{
System.out.println("I got a label " + cell.getContents());
}
if (type == CellType.NUMBER)
{
System.out.println("I got a number " + cell.getContents());
}
}
}
}
catch (final BiffException e)
{
e.printStackTrace();
}
}
When I am uploading file through browse button and click on upload. It is giving Casting error
HTTP Status 500 - Request processing failed; nested exception is java.lang.ClassCastException: org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest
I can't figure out where I am doing wrong. Please help.
المحلول
Please add Multipart Resolver in your spring config (app config) like below:
<bean id="multiPartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>