springmvc.xml
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 支持的其中一个属性,支持的最大文件大小,以字节为单位 这是100个g-->
<property name="maxUploadSize" value="107374182400"></property>
<!-- 设置默认的编码格式 -->
<property name="defaultEncoding" value="utf-8"></property>
</bean>
接收参数加上 @RequestParam("file") MultipartFile file
判断是否为空上传文件逻辑
if (!file.isEmpty()) {
try {
String originalFilename = file.getOriginalFilename();
originalFilename=originalFilename.substring(originalFilename.lastIndexOf("."));//获取文件扩展名!
// 文件保存路径,放在web-inf目录下面的uploud目录名字为uuid自动生成
String filePath = request.getSession().getServletContext().getRealPath("/") + "WEB-INF/upload/"
+ UUID.randomUUID().toString() + originalFilename;
// 转存文件
System.out.println(filePath);
file.transferTo(new java.io.File(filePath));
} catch (Exception e) {
e.printStackTrace();
}
}