前言
@RequestMapping("/fileUpload2")
public String fileUpload2(HttpServletRequest request, MultipartFile upload) throws Exception {
System.out.println("文件上传");
String realPath = request.getSession().getServletContext().getRealPath("/uploads/");
System.out.println(realPath);
File file = new File(realPath);
if (!file.exists()){
file.mkdirs();
}
String fileName = upload.getName();
String uuid = UUID.randomUUID().toString().replace("-", "");
fileName = uuid+"_"+fileName;
upload.transferTo(new File(realPath,fileName));
return "success";
}
文件解析器
<!--配置文件解析器:名字唯一,自动解析request获取上传文件通过总控制器传给controller中得方法定义时要有Multipartfile项接受,注意名字得和表单中的相同-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--设置最大上传大小 value是设置大小以字节为单位-->
<property name="maxUploadSize" value="10485760" />
</bean>