SpringMVC+ExtJS3.x实现文件上传
第一步:
配置文件applicationContext.xml中添加解析类bean
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"></property>
</bean>
控制器实现:
@Controller
public class FileController {
@RequestMapping("/upload")
public String doUpload(@RequestParam(value = "F_FileType", required = false) String sType,HttpServletRequest request){
MultipartHttpServletRequest fileRequest = (MultipartHttpServletRequest) request;
MultipartFile file = fileRequest.getFile("F_PicPath");
String path = request.getSession().getServletContext().getRealPath("upload")+"\\"+sType+"\\";
String fileName = file.getOriginalFilename();
File targetFile = new File(path, fileName);
if (!targetFile.exists()){
targetFile.mkdirs();
}
try{
file.transferTo(targetFile);
}catch(Exception e){
e.printStackTrace();
}
return "result";
}
}
ExtJS 界面调用:
在FormPanel中添加属性 fileUpload : true,
提交需要用form提交:
function doSubmitFile(){
//var fileNameObj = Ext.getCmp('F_PicPath');
var fileNameObj = Ext.getCmp('F_PicPath');
housedoor_center_form.getForm().submit({
url : "upload.do",
waitMsg : '正在保存数据...',
params:{
"F_FileType":'1'
},
success : onAddSuccess,
failure : onFailure
});
}