当我使用spring mvc代码时,我一直收到此错误我正在尝试使用spring mvc进行图像上传,这是我缺少的参数。
org.springframework.web.util.NestedServletException: Request processing failed;
nested exception is java.lang.IllegalArgumentException: argument type mismatch
java.lang.IllegalArgumentException: argument type mismatch
...
我的调度程序servlet是
<context:component-scan base-package="com.ImageUploadSpring.Controller" />
<!-- <bean id="simpleHandler" class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/Upload.html">FileUpload</prop>
</props>
</property>
</bean>
<bean id="FileUpload" class="com.ImageUploadSpring.Controller.FileUpload">
<property name="commandName" value="ImageUpload"/>
<property name="commandClass" value="com.ImageUploadSpring.Bean.UploadItem"/>
<property name="formView" value="ImageUpload"/>
<property name="successView" value="message"/>
</bean>
<bean id="FileUpload" class="com.ImageUploadSpring.Controller.FileUpload"></bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".html" />
</bean>
控制器类
public class FileUpload extends SimpleFormController{
@RequestMapping(value = "/Upload.html", method = RequestMethod.POST)
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors,HttpSession session) {
System.out.println("inside submit method");
try{
UploadItem item=(UploadItem)command;
MultipartFile file = item.getFile();
InputStream inputStream = null;
OutputStream outputStream = null;
if (file.getSize() > 0) {
inputStream = file.getInputStream();
outputStream = new FileOutputStream("D:/UploadedFiles/Images/"
+ file.getOriginalFilename());
System.out.println(file.getOriginalFilename());
int readBytes = 0;
byte[] buffer = new byte[8192];
while ((readBytes = inputStream.read(buffer, 0, 8192)) != -1) {
outputStream.write(buffer, 0, readBytes);
}
outputStream.close();
inputStream.close();
session.setAttribute("uploadFile", "D:/UploadedFiles/Images/"
+ file.getOriginalFilename());
}
}catch (Exception e) {
e.printStackTrace();
}
return new ModelAndView("message");
}
@Override
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder)
throws ServletException {
binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
}
我的html页面是
<form name="ImageUpload" action="/ImageUploadSpring/service/Upload.html" method="POST" enctype="multipart/form-data">
<div>
Select images:
<input type="text" id="box"/>
<input type="file" id="UploadFile" name="UploadFile" onchange="CopyMe(this,'box');" accept="image/*" size="40" style="width: 91px;" multiple />
<br><br>
<input type="submit" value="Upload" /><br><br>
</div>
</form>
答案 0 :(得分:1)
试试这个:
protected ModelAndView onSubmit(HttpServletRequest request,HttpServletResponse response,@RequestParam(value="UploadFile") MultipartFile image, BindException errors,HttpSession session)
答案 1 :(得分:0)
在定义<input type="file">
时,您已指定名称name="UploadFile"
。而在您的UploadItem命令对象中,文件属性为file
(从item.getFile()猜测)。您确定正确映射文件名吗?
请参阅this tutorial了解有关Spring MVC文件上传的工作教程
答案 2 :(得分:0)
本教程http://www.journaldev.com/2573/spring-mvc-file-upload-example-tutorial-single-and-multiple-files对我也很有用。这很简单。 重要的是当你使用
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="100000" />
</bean>
, 不要忘记将commons-fileupload添加到你的pom依赖项中。