springmvc多文件上传

来源:互联网 发布:css编程放在html 编辑:程序博客网 时间:2024/05/22 06:33
//添加用户时,为post请求,访问以下代码@RequestMapping(value="/adduser",method=RequestMethod.POST)public String add(@Validated User user,BindingResult bindingResult,@RequestParam("attachs") MultipartFile[] attachs,HttpServletRequest req){//紧跟validate之后写验证结果if(bindingResult.hasErrors()){//若有错误,直接掉转到add 视图return "user/add";}String realPath =  req.getSession().getServletContext().getRealPath("/resources/upload");System.out.println(realPath);for(MultipartFile attach:attachs){if(!attach.isEmpty()){//判断文件是否为空File file = new File(realPath+"/"+attach.getOriginalFilename());try {//FileUtils.copyInputStreamToFile(attach.getInputStream(), file);//FileUtils.writeByteArrayToFile(file, attach.getBytes());attach.transferTo(file);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}userList.put(user.getUserName(), user);return "redirect:/user/userlist";}
<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%@taglib prefix="sf" uri="http://www.springframework.org/tags/form" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>add</title></head><body><!-- 没有写action,直接提交给/add --><sf:form method="post" modelAttribute="user" enctype="multipart/form-data">UserName:<sf:input path="userName"/><sf:errors path="userName"/><br/>password:<sf:password path="password"/><sf:errors path="password"/><br/>position:<sf:input path="position"/><br/>email:<sf:input path="email"/><sf:errors path="email"/><br/>Attach1:<input type="file" name="attachs"/><br/>Attach2:<input type="file" name="attachs"/><br/>Attach3:<input type="file" name="attachs"/><br/>Attach4:<input type="file" name="attachs"/><br/><input type="submit" value="保存"/></sf:form></body></html>

//多文件上传必须要加@RequestParam
@RequestMapping(value="/upload",method=RequestMethod.POST)
public String upload(@RequestParam MultipartFile[] attachs,HttpServletRequest request){
 //指定上传位置 servletContext realpath从request中得
  String uploadFilePath =  request.getSession().getServletContext().getRealPath("/statics/upload");
  
  
  try {
for(MultipartFile attach : attachs){
if(!attach.isEmpty()){
//自动识别windows和Linux 路径自适应分隔符 File.separator
File saveFile = new File(uploadFilePath+File.separator+attach.getOriginalFilename());
//(源文件, 目标文件路径)自动创建路径
FileUtils.copyInputStreamToFile(attach.getInputStream(),saveFile);
  
}
}
} catch (IOException e) {
e.printStackTrace();
}
  return "welcome";
  
}

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><link href="<%=request.getContextPath() %>/statics/css/main.css" rel="stylesheet" style="text/css"/><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><form action="user/upload" method="post" enctype="multipart/form-data">上传文件:<input type="file" name="attachs"/><br/><input type="file" name="attachs"/><br/><input type="file" name="attachs"/><br/><input type="submit" value="登录"/></form></body>
0 0