<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/user/moreupload.action" method="post" enctype="multipart/form-data">
<input type="text" name="wenben" value=""/><br>
<input type="file" name="file" /><br>
<input type="file" name="file" /><br>
<input type="submit" value="文件上传" />
</form>
</body>
</html>
@RequestMapping("/user")
@Controller
public class duowenjianshangchuan {
/**
* file:传入的参数名称 MultipartFile:上传文件的对象,它会标识文件的类型。
*
* @param multipartFile
* @return String : 返回简写视图名称
* @throws Exception
*/
@RequestMapping("/moreupload")
public String upload(@RequestParam("file") MultipartFile[] multipartFile, HttpServletRequest request)
throws Exception {
String string = request.getParameter("wenben");
System.out.println(string);
for (MultipartFile myfile : multipartFile) {
if (myfile.isEmpty()) {
System.out.println("文件未上传");
} else {
System.out.println("文件长度: " + myfile.getSize());
System.out.println("文件类型: " + myfile.getContentType());
System.out.println("文件名称: " + myfile.getName());
System.out.println("文件原名: " + myfile.getOriginalFilename());
System.out.println("========================================");
String realPath = request.getSession().getServletContext().getRealPath("/WEB-INF/upload");
System.out.println("上传到tomcat服务器的地址" + realPath);
}
}
return "redirect:/html/success.html";
}
@RequestMapping("/moreupload2")
public String upload2(HttpServletRequest request) throws Exception {
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
List<MultipartFile> files = multiRequest.getFiles("file");
for (int i = 0; i < files.size(); i++) {
System.out.println(files.get(i).getOriginalFilename());
}
return "redirect:/html/success.html";
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>上传图片</title>
<script src="/jquery-3.2.1.js"></script>
</head>
<body>
<div>
<input type="file" class="easyui-linkbutton" id="excelFile" name="excelFile" size="20" maxlength="20"/>
<input type="button" class="easyui-linkbutton" id="upload" style="width:80px" value="上传" />
</div>
</body>
<script>
$(document).ready(function(){
function ajaxFileUpload(){
var formData = new FormData();
formData.append('file',$("#excelFile")[0].files[0]);
$.ajax({
type:"post",
url:"http://localhost:8080/fileupload",
async:false,
contentType: false,
processData: false,
data:formData,
dataType:'text',
success:function(data){
alert(data);
},
error:function(XMLHttpRequest, textStatus, errorThrown, data){
alert(errorThrown);
}
});
}
$("#upload").click(function(){
ajaxFileUpload();
});
});
</script>
</html>
@RequestMapping(value="fileupload", method=RequestMethod.POST,produces="text/html;charset=utf-8")
public void addPic(HttpServletResponse response,HttpServletRequest request,
@RequestParam(value="file", required=false) MultipartFile file) throws IOException{
System.out.println(file.getOriginalFilename());
response.getWriter().write("success");
response.setHeader("Access-Control-Allow-Origin", "*");
}
三.参考
https://www.cnblogs.com/emperorking/articles/6396956.html