1.Java代码部分:
1.1spring配置:
<!-- 定义文件上传解析器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设定默认编码 -->
<property name="defaultEncoding" value="UTF-8"></property>
<!-- 设定文件上传的最大值5MB,5*1024*1024 -->
<property name="maxUploadSize" value="5242880"></property>
</bean>
1.2Controller层代码(上传单个文件)
@RequestMapping("/upload")
public String uploadFile(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
if (!file.isEmpty()) {
try {
String path = request.getServletContext().getRealPath("/upload/archive/");
System.out.println("String path = " + path);
File dir = new File(path + File.separator + "老人111");
if (!dir.exists()) {
dir.mkdirs();
}
File serverFile = new File(dir.getAbsolutePath() + File.separator + file.getOriginalFilename());
file.transferTo(serverFile);
return "You successfully uploaded file=" + file.getOriginalFilename();
} catch (Exception e) {
return "You failed to upload " + file.getOriginalFilename() + " => " + e.getMessage();
}
} else {
return "You failed to upload " + file.getOriginalFilename() + " because the file was empty.";
}
}
1.3Controller层代码(单个input上传多个文件)
@RequestMapping("/uploadManyFile")
public String uploadManyFile(@RequestParam("file") MultipartFile[] files,
if (files.length != 0) {
try {
for (MultipartFile file : files) {
String path = request.getServletContext().getRealPath("/upload/archive/");
File dir = new File(path + File.separator + "老人222");
if (!dir.exists()) {
dir.mkdirs();
}
File serverFile = new File(dir.getAbsolutePath() + File.separator + file.getOriginalFilename());
file.transferTo(serverFile);
}
return "成功了";
} catch (Exception e) {
return "上传失败了,异常:" + e.getMessage();
}
} else {
return "空的嗷";
}
}
1.4Controller层代码(多个input,每个input上传若干个文件)
@RequestMapping("/uploadManyFile")
public String uploadManyFile(@RequestParam("files1") MultipartFile[] files1,
@RequestParam("files2") MultipartFile[] files2
,HttpServletRequest request) {
if (files1.length != 0 && files2.length != 0) {
try {
for (MultipartFile file : files1) {
String path = request.getServletContext().getRealPath("/upload/archive/");
File dir = new File(path + File.separator + "老人333" + File.separator +"身份证");
if (!dir.exists()) {
dir.mkdirs();
}
File serverFile = new File(dir.getAbsolutePath() + File.separator + file.getOriginalFilename());
file.transferTo(serverFile);
}
for (MultipartFile file : files2) {
String path = request.getServletContext().getRealPath("/upload/archive/");
File dir = new File(path + File.separator + "老人333" + File.separator +"正明");
if (!dir.exists()) {
dir.mkdirs();
}
File serverFile = new File(dir.getAbsolutePath() + File.separator + file.getOriginalFilename());
file.transferTo(serverFile);
}
return "成功了";
} catch (Exception e) {
return "上传失败了,异常:" + e.getMessage();
}
} else {
return "空的嗷";
}
}
2.页面部分
2.1上传单个文件页面
<html>
<head>
<title>Title</title>
</head>
<body>
<form enctype="multipart/form-data" action="${pageContext.request.contextPath}/home/upload" method="post">
<input type="file" name="file" >
<input type="submit" value="提交">
</form>
</body>
</html>
2.2单个input(type=file)上传多个文件页面
<html>
<head>
<title>Title</title>
</head>
<body>
<form enctype="multipart/form-data" action="${pageContext.request.contextPath}/home/uploadManyFile" method="post">
<input type="file" name="file" multiple>
<input type="submit" value="提交">
</form>
</body>
</html>
2.3多个input,每个input上传若干个文件页面
<html>
<head>
<title>Title</title>
</head>
<body>
<form enctype="multipart/form-data" action="${pageContext.request.contextPath}/home/uploadManyFile" method="post">
<input type="file" name="files1" multiple>
<input type="file" name="files2" multiple>
<input type="submit" value="提交">
</form>
</body>
</html>