Spring MVC 文件上传

突然之间很痛恨那些用采集程序做的垃圾站,遇到个问题搜索一下出来几百个网页,打开一个看看解决方案,看半天最后还是对不上号,再看下一个,晕!和前面的文章一样,再换还是一样…本来人就挺急的,打开一个重复的,打开一个重复的,标题不一样,内容全一样,FUCK!最后没办法,问题翻译成英文(都是程序上的问题,基本上不需要翻译。)去国外的网站上去找,找到了。
入正题。UI上用ExtJS,后台使用Spring的SimpleFormController进行数据处理。
Controller数据处理部分代码:

protected ModelAndView onSubmit(HttpServletRequest request,
		HttpServletResponse response, Object command, BindException errors)
		throws Exception {
 
	response.setContentType("text/html;charset=UTF-8");
	Plans plan = (Plans)command;
	CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
	commonsMultipartResolver.setDefaultEncoding("utf-8");
	if(commonsMultipartResolver.isMultipart(request))
	{
		MultipartHttpServletRequest multipartRequest = commonsMultipartResolver.resolveMultipart(request);
		MultipartFile ffile = multipartRequest.getFile("planFile");
		if(ffile != null){
			File tfile = new File("d:\\"+ffile.getOriginalFilename());
			ffile.transferTo(tfile);
		}
	 }
	this.planService.savePlan(plan);
	response.getWriter().write("{'success':true,'msg':'新建协同计划成功'}");
	return null;
}

当把FormPanel的fileUpload置为true时,文件被写入D盘,但是command对象中没有表单中的数据,反之则command封装了表单数据可文件无法上传(废话!- -!)。起初以为是FileUploadField.js这个扩展的问题,但不是。
查资料说是要在配置文件中加入如下配置启用CommonsMultipartResolver即可解决。

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
	<property name="maxUploadSize">
		<value>3455340</value>
	</property>
	<property name="defaultEncoding">
		<value>utf-8</value>
	</property>
</bean>

可加完后我感觉想哭,command中有数据了,但是文件又传不上去了(文章作者还说经过测试成功)。再查…
当加了上面的配置后,Spring会自动将onSubmit()方法中的HttpServletRequest转为MultipartHttpServletRequest,onSubmit()中加了句
System.out.println(request instanceof MultipartHttpServletRequest);
输出true,应该没错了。
重新修改onSubmit()方法如下:

protected ModelAndView onSubmit(HttpServletRequest request,
		HttpServletResponse response, Object command, BindException errors)
		throws Exception {
 
	response.setContentType("text/html;charset=UTF-8");
	Plans plan = (Plans)command;
	MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
	MultipartFile ffile = multipartRequest.getFile("planFile");
	if(ffile != null){
 
		String ffileName = ffile.getOriginalFilename();
		String tfileName = UUID.randomUUID().toString()+ffileName.substring(ffileName.lastIndexOf('.'), ffileName.length());
		File tfile = new File(request.getSession().getServletContext().getRealPath("")+"\\upload\\"+tfileName);
		ffile.transferTo(tfile);
		plan.setSdocname(ffileName);
		plan.setTdocname(tfileName);
	}
	this.planService.savePlan(plan);
	response.getWriter().write("{'success':true,'msg':'新建协同计划成功'}");
	return null;
}

搞定。


除非另有声明,本站遵循【署名-非商业性使用-相同方式共享 3.0 共享协议】授权。

转载原创文章请注明,转载自:Neeke[http://www.ineeke.com]

本文链接: http://www.ineeke.com/archives/spring-mvc-file-upload/

2009年11月26日 | 归档于 J2EE技术 | 5 条评论
  1. a2234
    2009年12月23日 15:56 | #1

    可以提供例子下载吗?我弄来弄去都不成功。

  2. 2009年12月1日 15:25 | #3

    你研究的很深了

  3. 2009年11月26日 16:28 | #5

    技术帖子了!!!

发表评论

XHTML: 您可以使用这些标签: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">
n:-zy n:-zr n:-zan n:-xf n:-wx n:-tz n:-tt n:-ts n:-sy n:-st n:-ss n:-sk n:-qd n:-pz n:-lh n:-kun n:-ku n:-hx n:-hd n:-gt n:-gg n:-bz

NOTICE: You should type some Chinese word (like “你好”) in your comment to pass the spam-check, thanks for your patience!