1.引入核心JAR文件
SpringMVC实现文件上传,需要再添加两个jar包。一个是文件上传的jar包,一个是其所依赖的IO包。这两个jar包,均在Spring支持库的org.apache.commons中。
2.spring-servlet.xml配置
<!-- 多部分文件上传 -->
<bean id=
"multipartResolver"
class
=
"org.springframework.web.multipart.commons.CommonsMultipartResolver"
>
<property name=
"maxUploadSize"
value=
"104857600"
/>
<property name=
"maxInMemorySize"
value=
"4096"
/>
<property name=
"defaultEncoding"
value=
"UTF-8"
></property>
</bean>
3.java代码
/*
* 采用file.Transto 来保存上传的文件
*/
@RequestMapping("/poiGetidsMedicineCodeUpdateRows")
@ResponseBody
public String poiGetidsMedicineCodeUpdateRows(@RequestParam("file") CommonsMultipartFile file, String poiGetids) throws IOException {
long startTime=System.currentTimeMillis();
System.out.println("fileName:"+ file.getOriginalFilename());
System.out.println(poiGetids);
String path="C:/"+new Date().getTime()+file.getOriginalFilename();
File newFile=new File(path);
//通过CommonsMultipartFile的方法直接写文件(注意这个时候)
file.transferTo(newFile);
long endTime=System.currentTimeMillis();
return "index2";
}