文件上传的步骤:
1.在index.jsp中写一个表单:
<span style="font-family:SimSun;font-size:24px;"> <form action="springmvc/testFileUpload" method="POST" <span style="color:#ff0000;">enctype="multipart/form-data"></span>
File: <input type="file" name="file"/>
Desc: <input type="text" name="desc"/>
<input type="submit" value="Submit"/>
</form></span>
2.写一个controller:
<span style="font-family:SimSun;font-size:24px;">package com.cgf.springmvc.handlers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
@RequestMapping(value="/springmvc")
@Controller
public class MyTestFileUpload {
@RequestMapping(value="/testFileUpload")
public String testFileUpload(<span style="color:#ff0000;">@RequestParam(value="file")MultipartFile file,</span>
@RequestParam(value="desc")String desc){
System.out.println(desc);
System.out.println("file name:"+file.getOriginalFilename());
return "success";
}
}</span>
3.在springmvc.xml中配置CommonsMultipartResolver
<span style="font-family:SimSun;font-size:24px;"><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"></property>
<property name="maxUploadSize" value="1024000"></property>
</bean></span>