springmvc上传
jar
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
配置文件
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"/>
<!-- 最大内存大小 -->
<property name="maxInMemorySize" value="10240"/>
<!-- 最大文件大小,-1为不限制大小 -->
<property name="maxUploadSize" value="-1"/>
</bean>
单个上传
页面
<form action="/ssm/save" method="post" enctype="multipart/form-data">
<label>图片</label><input type="file" name="file"/><br/>
<input type="submit" value="提 交"/>
</form>
控制器
@RequestMapping("save")
public Object save(HttpServletRequest request, @RequestParam(value = "file", required = false) MultipartFile file) {
// 如果上传的图片不为空
if (!file.isEmpty()) {
// 原始名称
String originalFilename = file.getOriginalFilename();
System.out.println("图片原始名称:" + originalFilename);
// 当前路径
String currentPath = request.getSession().getServletContext().getRealPath("");
System.out.println("当前路径" + currentPath);
// 新的图片名称
String newFileName = System.currentTimeMillis()
+ originalFilename.substring(originalFilename.lastIndexOf("."));
System.out.println("图片新名称:" + newFileName);
// 创建图片存储路径
File imgPath = new File(currentPath + "/image/");
System.out.println("图片地址:" + imgPath);
// 如果该路径不存在则创建一个新路径
if (!imgPath.exists()) {
imgPath.mkdirs();
}
// 图片
File newFile = new File(imgPath + "/" + newFileName);
// 将内存中的数据写入
try {
file.transferTo(newFile);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("没有图片");
}
return "redirect:show";
}
多个上传
页面
<form action="/ssm1/uploads" method="Post" enctype="multipart/form-data">
<input type="file" name="file"><br>
<input type="file" name="file"><br>
<input type="file" name="file"><br>
<input type="submit">
</form>
控制器
@PostMapping(value = "/uploads")
public String save(@RequestParam(value = "file", required = false) MultipartFile[] file,
HttpServletRequest request) {
//判断第一个图片是否为空
if (!file[0].isEmpty()) {
// 存储当前路径
String currentPath = request.getSession().getServletContext().getRealPath("");
System.out.println("当前路径:" + currentPath);
// 创建图片存储目录
File imgPath = new File(currentPath + "image/");
System.out.println("图片目录:" + imgPath);
if (!imgPath.exists()) {
imgPath.mkdirs();
}
// 循环遍历file
for (int i = 0; i < file.length; i++) {
// 原始名称
String originalFilename = file[i].getOriginalFilename();
System.out.println("原始名称:" + originalFilename);
// 重新命名 加i是为了区别名称
String newFileName = System.currentTimeMillis() + i
+ originalFilename.substring(originalFilename.lastIndexOf("."));
System.out.println("图片新名称:" + newFileName);
File newFile = new File(imgPath + "/" + newFileName);
System.out.println("图片地址:" + newFile);
try {
file[i].transferTo(newFile);
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}else{
System.out.println("没有图片");
}
return "show";
}