SpringMVC MultipartResolver 配置
conntroller 层
<!-- SpringMVC上传文件时,需要指定MultipartResolver处理器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8"></property> <!-- 指定 上传文件的总大小不能超过10M,注意:maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量总和 --> <property name="maxUploadSize" value="10485760" /> </bean>
JSP页面表格form 表单 (异步请求还不知道如何进行编写)
<form action="/activityInfoLoad/upload.do" method="post" enctype="multipart/form-data">
<label>选择本地文件:</label>
<input type="file" name="local_file" id="local_file"/>
<input type="text" name="file_path" id="file_path" value="${filepath}" width="400"/>
<input type="submit" value="上传文件"/>
</form>
conntroller 层
@RequestMapping("/upload.do")
public String upload(@RequestParam("local_file")MultipartFile file,Model model) throws IOException, InvocationTargetException {
// String path11 = request.getSession().getServletContext().getRealPath("upload");
String name=file.getOriginalFilename();//文件原始名称
String path = FileConfig.getUploadQrcodePath()+"/file";//文件存储路径
File dir=new File(path,name);
if(!dir.exists())//目录不存在 进行目录创建
{
dir.mkdirs();
}
file.transferTo(dir);//文件传输到指定目录
model.addAttribute("result",msg);//返回前端页面结果
return activiy_info_list;
}