在beans文件中要加入
    <bean id="multipartResolver"
          
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
          
<description>
                  ifpre::要使用文件上传功能,必须在定义文件中加入MeltipartResolver,
                  可以选用CommonsMutipartResolver或CosMultipartResolver
          
</description>
        
<property name="maxUploadSize">
            
<value>1000000</value>
        
</property>
    
</bean>

同时上传文件使用到相依赖的Commands FileUpload,所以要在lib目录中包括commons-fileupload.jar文件。

写完后调试的过程中,老报错:java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream,少了commons-io.jar,加上后,就ok了

在Controller端,可以用以下方式取得上传的文件,可以上传多个文件:
 public Set getMultipartFileForForm(HttpServletRequest req) {
  MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) req;
  // CommonsMultipartFile file = (CommonsMultipartFile)
  // multipartRequest.getFile("attach");//attach为form为file的id值
  // MultipartHttpServletRequest multipartRequest =
  // (MultipartHttpServletRequest)request;
  Set fileset = new LinkedHashSet();
  for (Iterator it = multipartRequest.getFileNames(); it.hasNext();) {
   String key = (String) it.next();
   MultipartFile file = multipartRequest.getFile(key);
   if (file.getOriginalFilename().length() > 0) {
    fileset.add(file);
   }
  }
  return fileset;
 }