-
Jul 4th, 2012, 12:41 AM
#1
Exception raises while file uploading
I am developing one application .here I required file uploading operation.while debug the application following exception is occured.
java.lang.ClassCastException: org.apache.catalina.connector.RequestFacade cannot be cast to org.springframework.web.multipart.MultipartHttpSer vletRequest
bugPostEnter.jsp:
...........................
<form:form method="post" commandName="bugDetailsVO1" name="frm" enctype="multipart/form-data">
<input type="hidden" name="products" value='<%=request.getParameter("products")%>'/ >
<input type="hidden" name="modules" value='<%=request.getParameter("modules")%>'/ >
<input type="hidden" name="submodule" value='<%=request.getParameter("submodule")%>'/ >
<input type="hidden" name="screens" value='<%=request.getParameter("screens")%>'/ >
<input type="hidden" name="testScenario" value='<%=request.getParameter("testScenario")%>'/ >
<input type="hidden" name="testcase" value='<%=request.getParameter("testcase")%>'/ >
<center>
<table><tr height='25pt'><td> </td></tr> </table>
<table cellpadding='0' cellspacing='0' border='0' width='95%' bordercolor='red'>
<tr> <td class='leftcorner' width='16' ><img src='images/blank.gif'></td>
<td bgcolor='#ffffff' width='98%'></td>
<td class='rightcorner' width='16'><img src="images/blank.gif"></td>
</tr>
<tr>
<td align='center' style='padding-top:5px;padding-bottom:0px' colspan='3' bgcolor='#ffffff'>
<table width="95%" cellspacing="0" cellpadding="0" border="0">
<tr height='30pt'><td colspan='2' align='left'> <FONT SIZE="2pt" COLOR="#FF9900"><B>BUG POSTING</B></FONT></td>
<td id="bugPostMsg" align ='center' style='display:none;background:transparent;'><font color='#00CC00' size="2pt">
Bug Posted Successfully With Bug Id: </font><span class="Form_Font"> ${model.bugId}
</td>
</tr>
</table>
<table cellspacing="0" cellpadding="0" border="0" bordercolor="red" width="95%" Class='Table_Border'>
<tr height='5pt'><td> </td></tr>
<tr height='30pt'>
<td align='left'><a href="javascript:displayFile();"><b> &n bsp; UPLOAD ATTACHMENT</b></a>
<input type=file name="file" id='fileId' style="display:none" readOnly><br> </td>
</tr>
...............................
<table border="0" cellpadding="0" cellspacing="0" >
<tr height='30pt'>
<td width="150px" align="center" valign='middle' >
<input type="button" class='Ecrm_But_3_Img_Css' value="Post Bug" onclick="validate()"/>
</td>
<td align="left" colspan="5" valign='middle' >
<input type="button" class='Ecrm_But_3_Img_Css' value="Back" onclick="navigateBack();"/>
</td>
</tr>
</table>
</form:form>
BugPostenterController.java:
try {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile multipartFile = multipartRequest.getFile("file");
/* MultipartFile multipartFile1 = multipartRequest.getFile("file1");
MultipartFile multipartFile2 = multipartRequest.getFile("file2");
MultipartFile multipartFile3 = multipartRequest.getFile("file3");
MultipartFile multipartFile4 = multipartRequest.getFile("file4");*/
OutputStream outputStream = null;
InputStream inputStream = null;
if (multipartFile.getOriginalFilename() !="") {
String f=multipartFile.getOriginalFilename();
String ext=multipartFile.getOriginalFilename().substring( multipartFile.getOriginalFilename().lastIndexOf(". ")+1, multipartFile.getOriginalFilename().length());
File dir = new File(request.getRealPath("/")
+ "uploads/"+multipartFile.getOriginalFilename());
String fileName = String.format("%s.%s", RandomStringUtils.randomNumeric(4), ext);
File file = new File(dir, fileName);
inputStream = multipartFile.getInputStream();
// File realUpload = new File("C:/");
outputStream = new FileOutputStream(request.getRealPath("/")
+ "uploads/"+fileName);
int readBytes = 0;
byte[] buffer = new byte[8192];
while ((readBytes = inputStream.read(buffer, 0, 8192)) != -1) {
outputStream.write(buffer, 0, readBytes);
/*try{
MagicMatch match = Magic.getMagicMatch(buffer);
if(match.getMimeType().contains("/"))
{
String filetype = match.getMimeType().split("/")[1];
if(!filetype.equalsIgnoreCase("jpg") && !filetype.equalsIgnoreCase("jpeg") && !filetype.equalsIgnoreCase("pdf") && !filetype.equalsIgnoreCase("msword"))
{
return new ModelAndView("bugPostEnter").addObject("statusKey" ,"msg.fileformatexp");
}
}
else{
return new ModelAndView("bugPostEnter").addObject("statusKey" ,"msg.fileformatexp");
}
}
catch(NoClassDefFoundError e)
{
e.printStackTrace();
}*/
}
outputStream.close();
inputStream.close();
session.setAttribute("uploadFile", request.getRealPath("/")
+ "uploads/"+multipartFile.getOriginalFilename());
}
try {
severityList = severityTypeService.getSeverityTypesAll();
}catch(Exception e){
severityList = null;
}
try {
bugTypeList = bugTypeService.getBugTypeAll();
}catch(Exception e){
bugTypeList = null;
}
try {
scenarioList = testCasePreparationService.getTestscenario();
}catch(Exception e){
scenarioList = null;
}
try {
testRoleList = roleTypeService.getRoleType(userName);
}catch(Exception e) {
testRoleList = null;
}
In Controller
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
at this line
java.lang.ClassCastException: org.apache.catalina.connector.RequestFacade cannot be cast to org.springframework.web.multipart.MultipartHttpSer vletRequest
exception is coming..I can't understand why this is happening.Please help me
thanks
-
Jul 4th, 2012, 01:20 AM
#2
Please use [ code][/code ] tags when posting code that way it remains readable.
If you want multipart support you need to configure a MultipartResolver in your configuration without it it isn't going to work. Also casting a request to a MultipartHttpServletRequest is never going to work as MultipartHttpServletRequest is a spring interface and has nothing to do with the standard servlet spec.
I strongly suggest a read of the file-upload chapter of the reference guide.
-
Dec 30th, 2012, 08:32 AM
#3
I would suggest you to follow this tutorial for file upload with Spring 3.0 MVC:
Upload files with Spring MVC (Eclipse-based tutorial)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules