java类代码(部分):
@RequestMapping(value= "/doUpload", method = RequestMethod.POST)
public String uploadFileHandler(HttpServletRequest request,Model model,
@RequestParam("file") MultipartFile[] files)
{
//Root Directory
String uploadRootPath = request.getServletContext().getRealPath("upload");
System.out.println("uploadRootPath=" + uploadRootPath);
File uploadRootDir = new File(uploadRootPath);
if(!uploadRootDir.exists())//Creat if it not exists
{
uploadRootDir.mkdirs();
}
List<File> uploadedFiles = new ArrayList<File>();
for (int i=0;i<files.length;i++)
{
MultipartFile file = files[i];
String name = file.getOriginalFilename();
System.out.println("Client File Name = " + name);
if(name != null && name.length() > 0)
{
try
{
byte[] bytes = file.getBytes();
File serverFile = new File(uploadRootDir.getAbsolutePath() + File.separator + name);
BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile));
stream.write(bytes);
stream.close();
uploadedFiles.add(serverFile);
System.out.println("Write file: "+ serverFile);
}
catch(Exception e)
{
System.out.println("Error Write file : "+ name);
}
}
}
model.addAttribute("uploadedFiles", uploadedFiles);
return "uploadResult";
}
jsp文件:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Upload Multi File</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h2>Upload Multiple File:</h2>
<form method="POST" action="doUpload" enctype="multipart/form-data">
File to upload (1): <input type="file" name="file"><br />
File to upload (2): <input type="file" name="file"><br />
File to upload (3): <input type="file" name="file"><br />
File to upload (4): <input type="file" name="file"><br />
File to upload (5): <input type="file" name="file"><br />
<input type="submit" value="Upload">
</form>
</body>
</html>
错误信息:
HTTP Status 400 - Required MultipartFile[] parameter 'file' is not present
type: Status report
message: Required MultipartFile[] parameter 'file' is not present
description: The request sent by the client was syntactically incorrect.
初学者,百思不得其解