使用springmvc框架实现多文件上传

需求;我们在添加用户的时候,需要上传员工的个人照片,还需要上传员工的身份证照片,这时候,就需要进行多文件的上传。

向数据库表smbms_user表中添加添加workpicpath

alter table smbms_user add workPicPath varchar(200)  null DEFAULT NULL COMMENT ‘上传个人工作证照片存储路径‘;--默认是将新加的字段添加到了原来表中最后一个字段的后面。

useradd.js

useradd.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@include file="/WEB-INF/jsp/common/head.jsp"%>

<div class="right">
        <div class="location">
            <strong>你现在所在的位置是:</strong>
            <span>用户管理页面 >> 用户添加页面</span>
        </div>
        <div class="providerAdd">
            <form id="userForm" name="userForm" method="post" action="${pageContext.request.contextPath }/user/useraddsave.html" enctype="multipart/form-data">
				<input type="hidden" name="method" value="add">
                <!--div的class 为error是验证错误,ok是验证成功-->
                <div>
                    <label for="userCode">用户编码:</label>
                    <input type="text" name="userCode" id="userCode" value="">
					<!-- 放置提示信息 -->
					<font color="red"></font>
                </div>
                <div>
                    <label for="userName">用户名称:</label>
                    <input type="text" name="userName" id="userName" value="">
					<font color="red"></font>
                </div>
                <div>
                    <label for="userPassword">用户密码:</label>
                    <input type="password" name="userPassword" id="userPassword" value="">
					<font color="red"></font>
                </div>
                <div>
                    <label for="ruserPassword">确认密码:</label>
                    <input type="password" name="ruserPassword" id="ruserPassword" value="">
					<font color="red"></font>
                </div>
                <div>
                    <label >用户性别:</label>
					<select name="gender" id="gender">
					    <option value="1" selected="selected">男</option>
					    <option value="2">女</option>
					 </select>
                </div>
                <div>
                    <label for="birthday">出生日期:</label>
                    <input type="text" Class="Wdate" id="birthday" name="birthday"
					readonly="readonly" onclick="WdatePicker();">
					<font color="red"></font>
                </div>
                <div>
                    <label for="phone">用户电话:</label>
                    <input type="text" name="phone" id="phone" value="">
					<font color="red"></font>
                </div>
                <!-- 两个 -->
                <div>
                    <input type="hidden" id="errorinfo" value="${uploadFileError }"/>
                    <label for="a_idPicPath">证件照:</label>
                    <input type="file" name="attachs" id="a_idPicPath">
					<font color="red"></font>
                </div>

                <div>
                    <input type="hidden" id="errorinfo_wp" value="${uploadwpFileError }"/>
                    <label for="a_workPicPath">免冠照:</label>
                    <input type="file" name="attachs" id="a_workPicPath">
					<font color="red"></font>
                </div>
                <div>
                    <label for="address">用户地址:</label>
                   <input name="address" id="address"  value="">
                </div>
                <div>
                    <label >用户角色:</label>
                    <!-- 列出所有的角色分类 -->
		    <!-- <select name="userRole" id="userRole"></select> -->
		    <select name="userRole" id="userRole">
			<option value="1">系统管理员</option>
			<option value="2">经理</option>
			<option value="3" selected="selected">普通用户</option>
		    </select>
	            <font color="red"></font>
                </div>
                <div class="providerAddBtn">
                    <input type="button" name="add" id="add" value="保存" >
					<input type="button" id="back" name="back" value="返回" >
                </div>
            </form>
        </div>
</div>
</section>
<%@include file="/WEB-INF/jsp/common/foot.jsp" %>
<script type="text/javascript" src="${pageContext.request.contextPath }/statics/js/useradd.js"></script>

 UserController.java

	// 其中request对象主要用来存放错误信息,便于前台获取进行相应的提示,现在是多文件上传
	@RequestMapping(value = "/useraddsave.html", method = RequestMethod.POST)
	public String addUserSave(
			User user,
			HttpSession session,
			HttpServletRequest request,
			@RequestParam(value = "attachs", required = false) MultipartFile[] attachs) {
		// 编写文件上传的代码
		// 1.判断上传的文件是否为空
		String idPicPath = null;
		String workPicPath = null;
		String errorInfo = null;// 定义统一的错误信息提示
		boolean flag = true;
		String path = request.getSession().getServletContext()
				.getRealPath("statics" + File.separator + "uploadfiles");
		logger.info("uploadFile path ============== > " + path);

		// 现在传过来的是多文件,使用的是数组,要进行循环遍历
		for (int i = 0; i < attachs.length; i++) {
			MultipartFile attach = attachs[i];
			// 如果上传的文件不为空 与系统有关的默认名称分隔符。此字段被初始化为包含系统属性 file.separator 的值的第一个字符。在
			// UNIX 系统上,此字段的值为 ‘/‘;在 Microsoft Windows 系统上,它为 ‘\\‘。
			if (i == 0) {
				errorInfo = "uploadFileError";
			} else if (i == 1) {
				errorInfo = "uploadwpFileError";
			}

			String oldFileName = attach.getOriginalFilename();// 原来你存在电脑盘符的文件名
			logger.info("原来的文件名 ============== > " + oldFileName);
			String prefix = FilenameUtils.getExtension(oldFileName);
			logger.info("上传文件的后缀:" + prefix);
			int filesize = 500000;// 表示文件大小是500k
			if (!attach.isEmpty()) {
				if (attach.getSize() > filesize) {
					request.setAttribute("errorInfo", "上传的文件大小不得超过500k");
					flag = false;
					// 判断文件的上传文件的格式
				} else if (prefix.equalsIgnoreCase("jpg")
						|| prefix.equalsIgnoreCase("png")
						|| prefix.equalsIgnoreCase("pneg")) {
					String fileName = System.currentTimeMillis()
							+ RandomUtils.nextInt(10000000) + "Personal.jpg";
					logger.debug("new fileName======== " + attach.getName());
					File targetFile = new File(path, fileName);
					logger.info("上传到服务器的文件名是:" + targetFile.toString());
					// 如果服务器的文件路径存在的话,就进行创建
					if (!targetFile.exists()) {
						/*
						 * mkdirs()可以建立多级文件夹, mkdir()只会建立一级的文件夹, 如下:
						 * new File("/tmp/one/two/three").mkdirs();
						 * 执行后, 会建立tmp/one/two/three四级目录
						 * new File("/tmp/one/two/three").mkdir();
						 * 则不会建立任何目录, 因为找不到/tmp/one/two目录, 结果返回false
						 */
						targetFile.mkdirs();
					}
					try {
						attach.transferTo(targetFile);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						request.setAttribute("errorInfo", "上传文件失败");
						flag = false;
					}
					// 文件路径
					if (i == 0) {
						idPicPath = path + File.separator + fileName;
					} else if (i == 1) {
						workPicPath = path + File.separator + fileName;
					}
					logger.debug("idPicPath: " + idPicPath);
					logger.debug("workPicPath: " + workPicPath);
				} else {
					request.setAttribute("errorInfo", " * 上传图片格式不正确");
					flag = false;
				}
			}
		}

		if (flag) {
			user.setCreatedBy(((User) session
					.getAttribute(Constants.USER_SESSION)).getId());
			user.setCreationDate(new Date());
			user.setIdPicPath(idPicPath);
			user.setWorkPicPath(workPicPath);
			if (userService.add(user)) {
				return "redirect:/user/userlist.html";
			}
		}
		return "useradd";
	}

  UserDaoImpl.java

package cn.smbms.dao.user;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.mysql.jdbc.StringUtils;

import cn.smbms.dao.BaseDao;
import cn.smbms.pojo.User;

/**
 * dao层抛出异常,让service层去捕获处理
 * @author Administrator
 *
 */
@Repository
public class UserDaoImpl implements UserDao{

	@Override
	public int add(Connection connection, User user) throws Exception {
		// TODO Auto-generated method stub
		PreparedStatement pstm = null;
		int updateRows = 0;
		if(null != connection){
			String sql = "insert into smbms_user (userCode,userName,userPassword," +
					"userRole,gender,birthday,phone,address,creationDate,createdBy,idPicPath,workPicPath) " +
					"values(?,?,?,?,?,?,?,?,?,?,?,?)";
			Object[] params = {user.getUserCode(),user.getUserName(),user.getUserPassword(),
							user.getUserRole(),user.getGender(),user.getBirthday(),
							user.getPhone(),user.getAddress(),user.getCreationDate(),user.getCreatedBy(),user.getIdPicPath(),user.getWorkPicPath()};
			updateRows = BaseDao.execute(connection, pstm, sql, params);
			BaseDao.closeResource(null, pstm, null);
		}
		return updateRows;
	}

	@Override
	public User getLoginUser(Connection connection, String userCode)
			throws Exception {
		// TODO Auto-generated method stub
		PreparedStatement pstm = null;
		ResultSet rs = null;
		User user = null;
		if(null != connection){
			String sql = "select * from smbms_user where userCode=?";
			Object[] params = {userCode};
			rs = BaseDao.execute(connection, pstm, rs, sql, params);
			if(rs.next()){
				user = new User();
				user.setId(rs.getInt("id"));
				user.setUserCode(rs.getString("userCode"));
				user.setUserName(rs.getString("userName"));
				user.setUserPassword(rs.getString("userPassword"));
				user.setGender(rs.getInt("gender"));
				user.setBirthday(rs.getDate("birthday"));
				user.setPhone(rs.getString("phone"));
				user.setAddress(rs.getString("address"));
				user.setUserRole(rs.getInt("userRole"));
				user.setCreatedBy(rs.getInt("createdBy"));
				user.setCreationDate(rs.getTimestamp("creationDate"));
				user.setModifyBy(rs.getInt("modifyBy"));
				user.setModifyDate(rs.getTimestamp("modifyDate"));
			}
			BaseDao.closeResource(null, pstm, rs);
		}
		return user;
	}

	@Override
	public List<User> getUserList(Connection connection, String userName,int userRole,int currentPageNo, int pageSize)
			throws Exception {
		// TODO Auto-generated method stub
		PreparedStatement pstm = null;
		ResultSet rs = null;
		List<User> userList = new ArrayList<User>();
		if(connection != null){
			StringBuffer sql = new StringBuffer();
			sql.append("select u.*,r.roleName as userRoleName from smbms_user u,smbms_role r where u.userRole = r.id");
			List<Object> list = new ArrayList<Object>();
			if(!StringUtils.isNullOrEmpty(userName)){
				sql.append(" and u.userName like ?");
				list.add("%"+userName+"%");
			}
			if(userRole > 0){
				sql.append(" and u.userRole = ?");
				list.add(userRole);
			}
			sql.append(" order by creationDate DESC limit ?,?");
			currentPageNo = (currentPageNo-1)*pageSize;
			list.add(currentPageNo);
			list.add(pageSize);

			Object[] params = list.toArray();
			System.out.println("sql ----> " + sql.toString());
			rs = BaseDao.execute(connection, pstm, rs, sql.toString(), params);
			while(rs.next()){
				User _user = new User();
				_user.setId(rs.getInt("id"));
				_user.setUserCode(rs.getString("userCode"));
				_user.setUserName(rs.getString("userName"));
				_user.setGender(rs.getInt("gender"));
				_user.setBirthday(rs.getDate("birthday"));
				_user.setPhone(rs.getString("phone"));
				_user.setUserRole(rs.getInt("userRole"));
				_user.setUserRoleName(rs.getString("userRoleName"));
				userList.add(_user);
			}
			BaseDao.closeResource(null, pstm, rs);
		}
		return userList;
	}

	@Override
	public int deleteUserById(Connection connection,Integer delId) throws Exception {
		// TODO Auto-generated method stub
		PreparedStatement pstm = null;
		int flag = 0;
		if(null != connection){
			String sql = "delete from smbms_user where id=?";
			Object[] params = {delId};
			flag = BaseDao.execute(connection, pstm, sql, params);
			BaseDao.closeResource(null, pstm, null);
		}
		return flag;
	}

	@Override
	public User getUserById(Connection connection, String id) throws Exception {
		// TODO Auto-generated method stub
		User user = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		if(null != connection){
			String sql = "select u.*,r.roleName as userRoleName from smbms_user u,smbms_role r where u.id=? and u.userRole = r.id";
			Object[] params = {id};
			rs = BaseDao.execute(connection, pstm, rs, sql, params);
			if(rs.next()){
				user = new User();
				user.setId(rs.getInt("id"));
				user.setUserCode(rs.getString("userCode"));
				user.setUserName(rs.getString("userName"));
				user.setUserPassword(rs.getString("userPassword"));
				user.setGender(rs.getInt("gender"));
				user.setBirthday(rs.getDate("birthday"));
				user.setPhone(rs.getString("phone"));
				user.setAddress(rs.getString("address"));
				user.setUserRole(rs.getInt("userRole"));
				user.setCreatedBy(rs.getInt("createdBy"));
				user.setCreationDate(rs.getTimestamp("creationDate"));
				user.setModifyBy(rs.getInt("modifyBy"));
				user.setModifyDate(rs.getTimestamp("modifyDate"));
				user.setUserRoleName(rs.getString("userRoleName"));
			}
			BaseDao.closeResource(null, pstm, rs);
		}
		return user;
	}

	@Override
	public int modify(Connection connection, User user) throws Exception {
		// TODO Auto-generated method stub
		int flag = 0;
		PreparedStatement pstm = null;
		if(null != connection){
			String sql = "update smbms_user set userName=?,"+
				"gender=?,birthday=?,phone=?,address=?,userRole=?,modifyBy=?,modifyDate=? where id = ? ";
			Object[] params = {user.getUserName(),user.getGender(),user.getBirthday(),
					user.getPhone(),user.getAddress(),user.getUserRole(),user.getModifyBy(),
					user.getModifyDate(),user.getId()};
			flag = BaseDao.execute(connection, pstm, sql, params);
			BaseDao.closeResource(null, pstm, null);
		}
		return flag;
	}

	@Override
	public int updatePwd(Connection connection, int id, String pwd)
			throws Exception {
		// TODO Auto-generated method stub
		int flag = 0;
		PreparedStatement pstm = null;
		if(connection != null){
			String sql = "update smbms_user set userPassword= ? where id = ?";
			Object[] params = {pwd,id};
			flag = BaseDao.execute(connection, pstm, sql, params);
			BaseDao.closeResource(null, pstm, null);
		}
		return flag;
	}

	@Override
	public int getUserCount(Connection connection, String userName, int userRole)
			throws Exception {
		// TODO Auto-generated method stub
		PreparedStatement pstm = null;
		ResultSet rs = null;
		int count = 0;
		if(connection != null){
			StringBuffer sql = new StringBuffer();
			sql.append("select count(1) as count from smbms_user u,smbms_role r where u.userRole = r.id");
			List<Object> list = new ArrayList<Object>();
			if(!StringUtils.isNullOrEmpty(userName)){
				sql.append(" and u.userName like ?");
				list.add("%"+userName+"%");
			}
			if(userRole > 0){
				sql.append(" and u.userRole = ?");
				list.add(userRole);
			}
			Object[] params = list.toArray();
			System.out.println("sql ----> " + sql.toString());
			rs = BaseDao.execute(connection, pstm, rs, sql.toString(), params);
			if(rs.next()){
				count = rs.getInt("count");
			}
			BaseDao.closeResource(null, pstm, rs);
		}
		return count;
	}

}

  UserServiceImpl.java

package cn.smbms.service.user;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import cn.smbms.dao.BaseDao;
import cn.smbms.dao.user.UserDao;
import cn.smbms.pojo.User;

/**
 * service层捕获异常,进行事务处理
 * 事务处理:调用不同dao的多个方法,必须使用同一个connection(connection作为参数传递)
 * 事务完成之后,需要在service层进行connection的关闭,在dao层关闭(PreparedStatement和ResultSet对象)
 * @author Administrator
 *
 */
@Service
public class UserServiceImpl implements UserService{
	@Resource
	private UserDao userDao;

	@Override
	public boolean add(User user) {
		// TODO Auto-generated method stub

		boolean flag = false;
		Connection connection = null;
		try {
			connection = BaseDao.getConnection();
			connection.setAutoCommit(false);//开启JDBC事务管理
			int updateRows = userDao.add(connection,user);
			connection.commit();
			if(updateRows > 0){
				flag = true;
				System.out.println("add success!");
			}else{
				System.out.println("add failed!");
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				System.out.println("rollback==================");
				connection.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}finally{
			//在service层进行connection连接的关闭
			BaseDao.closeResource(connection, null, null);
		}
		return flag;
	}
	@Override
	public User login(String userCode, String userPassword) {
		// TODO Auto-generated method stub
		Connection connection = null;
		User user = null;
		try {
			connection = BaseDao.getConnection();
			user = userDao.getLoginUser(connection, userCode);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			BaseDao.closeResource(connection, null, null);
		}

		//匹配密码
		if(null != user){
			if(!user.getUserPassword().equals(userPassword))
				user = null;
		}

		return user;
	}
	@Override
	public List<User> getUserList(String queryUserName,int queryUserRole,int currentPageNo, int pageSize) {
		// TODO Auto-generated method stub
		Connection connection = null;
		List<User> userList = null;
		System.out.println("queryUserName ---- > " + queryUserName);
		System.out.println("queryUserRole ---- > " + queryUserRole);
		System.out.println("currentPageNo ---- > " + currentPageNo);
		System.out.println("pageSize ---- > " + pageSize);
		try {
			connection = BaseDao.getConnection();
			userList = userDao.getUserList(connection, queryUserName,queryUserRole,currentPageNo,pageSize);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			BaseDao.closeResource(connection, null, null);
		}
		return userList;
	}
	@Override
	public User selectUserCodeExist(String userCode) {
		// TODO Auto-generated method stub
		Connection connection = null;
		User user = null;
		try {
			connection = BaseDao.getConnection();
			user = userDao.getLoginUser(connection, userCode);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			BaseDao.closeResource(connection, null, null);
		}
		return user;
	}
	@Override
	public boolean deleteUserById(Integer delId) {
		// TODO Auto-generated method stub
		Connection connection = null;
		boolean flag = false;
		try {
			connection = BaseDao.getConnection();
			if(userDao.deleteUserById(connection,delId) > 0)
				flag = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			BaseDao.closeResource(connection, null, null);
		}
		return flag;
	}
	@Override
	public User getUserById(String id) {
		// TODO Auto-generated method stub
		User user = null;
		Connection connection = null;
		try{
			connection = BaseDao.getConnection();
			user = userDao.getUserById(connection,id);
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			user = null;
		}finally{
			BaseDao.closeResource(connection, null, null);
		}
		return user;
	}
	@Override
	public boolean modify(User user) {
		// TODO Auto-generated method stub
		Connection connection = null;
		boolean flag = false;
		try {
			connection = BaseDao.getConnection();
			if(userDao.modify(connection,user) > 0)
				flag = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			BaseDao.closeResource(connection, null, null);
		}
		return flag;
	}
	@Override
	public boolean updatePwd(int id, String pwd) {
		// TODO Auto-generated method stub
		boolean flag = false;
		Connection connection = null;
		try{
			connection = BaseDao.getConnection();
			if(userDao.updatePwd(connection,id,pwd) > 0)
				flag = true;
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			BaseDao.closeResource(connection, null, null);
		}
		return flag;
	}
	@Override
	public int getUserCount(String queryUserName, int queryUserRole) {
		// TODO Auto-generated method stub
		Connection connection = null;
		int count = 0;
		System.out.println("queryUserName ---- > " + queryUserName);
		System.out.println("queryUserRole ---- > " + queryUserRole);
		try {
			connection = BaseDao.getConnection();
			count = userDao.getUserCount(connection, queryUserName,queryUserRole);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			BaseDao.closeResource(connection, null, null);
		}
		return count;
	}

}

 运行结果:

运行日志:

 (475854 ms) - 2020-1-30 16:50:59[DEBUG](FrameworkServlet.java:983) Successfully completed request
- (505303 ms) - 2020-1-30 16:51:28[DEBUG](DispatcherServlet.java:819) DispatcherServlet with name ‘springmvc‘ processing POST request for [/SMBMS_C11_01/user/useraddsave.html]
- (505310 ms) - 2020-1-30 16:51:28[DEBUG](CommonsFileUploadSupport.java:259) Found multipart file [attachs] of size 423722 bytes with original filename [微信图片_20200130152415.png], stored at [D:\SoftWare\tomcat\apache-tomcat-6.0.53\work\Catalina\localhost\SMBMS_C11_01\upload_3a923b64_16ff5a10d73__8000_00000032.tmp]
- (505310 ms) - 2020-1-30 16:51:28[DEBUG](CommonsFileUploadSupport.java:259) Found multipart file [attachs] of size 33326 bytes with original filename [sunFlower.jpg], stored at [D:\SoftWare\tomcat\apache-tomcat-6.0.53\work\Catalina\localhost\SMBMS_C11_01\upload_3a923b64_16ff5a10d73__8000_00000033.tmp]
- (505310 ms) - 2020-1-30 16:51:28[DEBUG](AbstractHandlerMethodMapping.java:229) Looking up handler method for path /user/useraddsave.html
- (505310 ms) - 2020-1-30 16:51:28[DEBUG](AbstractHandlerMethodMapping.java:234) Returning handler method [public java.lang.String cn.smbms.controller.UserController.addUserSave(cn.smbms.pojo.User,javax.servlet.http.HttpSession,javax.servlet.http.HttpServletRequest,org.springframework.web.multipart.MultipartFile[])]
- (505311 ms) - 2020-1-30 16:51:28[DEBUG](AbstractBeanFactory.java:243) Returning cached instance of singleton bean ‘userController‘
- (505312 ms) - 2020-1-30 16:51:28[ INFO](UserController.java:193) uploadFile path ============== > D:\SoftWare\tomcat\apache-tomcat-6.0.53\webapps\SMBMS_C11_01\statics\uploadfiles
- (505312 ms) - 2020-1-30 16:51:28[ INFO](UserController.java:207) 原来的文件名 ============== > 微信图片_20200130152415.png
- (505312 ms) - 2020-1-30 16:51:28[ INFO](UserController.java:209) 上传文件的后缀:png
- (505312 ms) - 2020-1-30 16:51:28[DEBUG](UserController.java:221) new fileName======== attachs
- (505312 ms) - 2020-1-30 16:51:28[ INFO](UserController.java:223) 上传到服务器的文件名是:D:\SoftWare\tomcat\apache-tomcat-6.0.53\webapps\SMBMS_C11_01\statics\uploadfiles\1580384074093Personal.jpg
- (505317 ms) - 2020-1-30 16:51:28[DEBUG](CommonsMultipartFile.java:139) Multipart file ‘attachs‘ with original filename [微信图片_20200130152415.png], stored at [D:\SoftWare\tomcat\apache-tomcat-6.0.53\work\Catalina\localhost\SMBMS_C11_01\upload_3a923b64_16ff5a10d73__8000_00000032.tmp]: moved to [D:\SoftWare\tomcat\apache-tomcat-6.0.53\webapps\SMBMS_C11_01\statics\uploadfiles\1580384074093Personal.jpg]
- (505317 ms) - 2020-1-30 16:51:28[DEBUG](UserController.java:249) idPicPath: D:\SoftWare\tomcat\apache-tomcat-6.0.53\webapps\SMBMS_C11_01\statics\uploadfiles\1580384074093Personal.jpg
- (505318 ms) - 2020-1-30 16:51:28[DEBUG](UserController.java:250) workPicPath: null
- (505318 ms) - 2020-1-30 16:51:28[ INFO](UserController.java:207) 原来的文件名 ============== > sunFlower.jpg
- (505318 ms) - 2020-1-30 16:51:28[ INFO](UserController.java:209) 上传文件的后缀:jpg
- (505318 ms) - 2020-1-30 16:51:28[DEBUG](UserController.java:221) new fileName======== attachs
- (505318 ms) - 2020-1-30 16:51:28[ INFO](UserController.java:223) 上传到服务器的文件名是:D:\SoftWare\tomcat\apache-tomcat-6.0.53\webapps\SMBMS_C11_01\statics\uploadfiles\1580376977645Personal.jpg
- (505320 ms) - 2020-1-30 16:51:28[DEBUG](CommonsMultipartFile.java:139) Multipart file ‘attachs‘ with original filename [sunFlower.jpg], stored at [D:\SoftWare\tomcat\apache-tomcat-6.0.53\work\Catalina\localhost\SMBMS_C11_01\upload_3a923b64_16ff5a10d73__8000_00000033.tmp]: moved to [D:\SoftWare\tomcat\apache-tomcat-6.0.53\webapps\SMBMS_C11_01\statics\uploadfiles\1580376977645Personal.jpg]
- (505320 ms) - 2020-1-30 16:51:28[DEBUG](UserController.java:249) idPicPath: D:\SoftWare\tomcat\apache-tomcat-6.0.53\webapps\SMBMS_C11_01\statics\uploadfiles\1580384074093Personal.jpg
- (505321 ms) - 2020-1-30 16:51:28[DEBUG](UserController.java:250) workPicPath: D:\SoftWare\tomcat\apache-tomcat-6.0.53\webapps\SMBMS_C11_01\statics\uploadfiles\1580376977645Personal.jpg
add success!
- (505329 ms) - 2020-1-30 16:51:28[DEBUG](DispatcherServlet.java:1198) Rendering view [org.springframework.web.servlet.view.RedirectView: name ‘redirect:/user/userlist.html‘; URL [/user/userlist.html]] in DispatcherServlet with name ‘springmvc‘
- (505329 ms) - 2020-1-30 16:51:28[DEBUG](CommonsFileUploadSupport.java:282) Cleaning up multipart file [attachs] with original filename [微信图片_20200130152415.png], stored at [D:\SoftWare\tomcat\apache-tomcat-6.0.53\work\Catalina\localhost\SMBMS_C11_01\upload_3a923b64_16ff5a10d73__8000_00000032.tmp]
- (505329 ms) - 2020-1-30 16:51:28[DEBUG](CommonsFileUploadSupport.java:282) Cleaning up multipart file [attachs] with original filename [sunFlower.jpg], stored at [D:\SoftWare\tomcat\apache-tomcat-6.0.53\work\Catalina\localhost\SMBMS_C11_01\upload_3a923b64_16ff5a10d73__8000_00000033.tmp]
- (505329 ms) - 2020-1-30 16:51:28[DEBUG](FrameworkServlet.java:983) Successfully completed request
- (505332 ms) - 2020-1-30 16:51:28[DEBUG](DispatcherServlet.java:819) DispatcherServlet with name ‘springmvc‘ processing GET request for [/SMBMS_C11_01/user/userlist.html]
- (505332 ms) - 2020-1-30 16:51:28[DEBUG](AbstractHandlerMethodMapping.java:229) Looking up handler method for path /user/userlist.html
- (505332 ms) - 2020-1-30 16:51:28[DEBUG](AbstractHandlerMethodMapping.java:234) Returning handler method [public java.lang.String cn.smbms.controller.UserController.getUserList(org.springframework.ui.Model,java.lang.String,java.lang.String,java.lang.String)]
- (505332 ms) - 2020-1-30 16:51:28[DEBUG](AbstractBeanFactory.java:243) Returning cached instance of singleton bean ‘userController‘
- (505332 ms) - 2020-1-30 16:51:28[DEBUG](DispatcherServlet.java:906) Last-Modified value for [/SMBMS_C11_01/user/userlist.html] is: -1
- (505333 ms) - 2020-1-30 16:51:28[ INFO](UserController.java:109) getUserList ---- > queryUserName: null
- (505333 ms) - 2020-1-30 16:51:28[ INFO](UserController.java:110) getUserList ---- > queryUserRole: null
- (505333 ms) - 2020-1-30 16:51:28[ INFO](UserController.java:111) getUserList ---- > pageIndex: null
queryUserName ---- >
queryUserRole ---- > 0
sql ----> select count(1) as count from smbms_user u,smbms_role r where u.userRole = r.id
queryUserName ---- >
queryUserRole ---- > 0
currentPageNo ---- > 1
pageSize ---- > 5
sql ----> select u.*,r.roleName as userRoleName from smbms_user u,smbms_role r where u.userRole = r.id order by creationDate DESC limit ?,?
- (505361 ms) - 2020-1-30 16:51:28[DEBUG](DispatcherServlet.java:1198) Rendering view [org.springframework.web.servlet.view.JstlView: name ‘userlist‘; URL [/WEB-INF/jsp/userlist.jsp]] in DispatcherServlet with name ‘springmvc‘
- (505362 ms) - 2020-1-30 16:51:28[DEBUG](AbstractView.java:375) Added model object ‘userList‘ of type [java.util.ArrayList] to request in view with name ‘userlist‘
- (505362 ms) - 2020-1-30 16:51:28[DEBUG](AbstractView.java:375) Added model object ‘roleList‘ of type [java.util.ArrayList] to request in view with name ‘userlist‘
- (505362 ms) - 2020-1-30 16:51:28[DEBUG](AbstractView.java:375) Added model object ‘queryUserName‘ of type [java.lang.String] to request in view with name ‘userlist‘
- (505362 ms) - 2020-1-30 16:51:28[DEBUG](AbstractView.java:382) Removed model object ‘queryUserRole‘ from request in view with name ‘userlist‘
- (505362 ms) - 2020-1-30 16:51:28[DEBUG](AbstractView.java:375) Added model object ‘totalPageCount‘ of type [java.lang.Integer] to request in view with name ‘userlist‘
- (505362 ms) - 2020-1-30 16:51:28[DEBUG](AbstractView.java:375) Added model object ‘totalCount‘ of type [java.lang.Integer] to request in view with name ‘userlist‘
- (505362 ms) - 2020-1-30 16:51:28[DEBUG](AbstractView.java:375) Added model object ‘currentPageNo‘ of type [java.lang.Integer] to request in view with name ‘userlist‘
- (505362 ms) - 2020-1-30 16:51:28[DEBUG](InternalResourceView.java:236) Forwarding to resource [/WEB-INF/jsp/userlist.jsp] in InternalResourceView ‘userlist‘
- (505367 ms) - 2020-1-30 16:51:28[DEBUG](FrameworkServlet.java:983) Successfully completed request

  

 

原文地址:https://www.cnblogs.com/dongyaotou/p/12243115.html

时间: 2024-10-02 00:29:55

使用springmvc框架实现多文件上传的相关文章

SpringMVC学习记录(四)--文件上传

学习一个框架少不了学习文件上传 在使用springMVC进行系统实现时,springMVC默认的解析器里面是没有加入对文件上传的解析的,这可以方便我们实现自己的文件上传.但如果你想使用springMVC对文件上传的解析器来处理文件上传的时候就需要在spring的applicationContext里面加上springMVC提供的MultipartResolver的申明.这样之后,客户端每次进行请求的时候,springMVC都会检查request里面是否包含多媒体信息,如果包含了就会使用Multi

SpringMVC(6)文件上传

SpringMVC(6)文件上传 我们做一个上传图片的例子,页面(fileUpload.jsp)功能如下: 上传成功后即时显示上传的图片. upload.jsp: <body> <h3>文件上传</h3> <form action="/testAnnotationMVC_fileUpload/file/fileUpload2.jspx" method="post" enctype="multipart/form-d

SpringMVC中的多文件上传

这是用的是SpringMVC-3.1.1.commons-fileupload-1.2.2和io-2.0.1 首先是web.xml <?xml version="1.0" encoding="UTF-8"?>   <web-app version="2.5"        xmlns="http://java.sun.com/xml/ns/javaee"        xmlns:xsi="http

SpringMVC注解方式与文件上传

目录: springmvc的注解方式 文件上传(上传图片,并显示) 一.注解 在类前面加上@Controller 表示该类是一个控制器在方法handleRequest 前面加上 @RequestMapping("/index") 表示路径/index会映射到该方法上 将上一篇的博客改为注解方式: SpringMVC的基础配置及视图定位 1.修改springmvc-servlet.xml 去掉映射相关的配置,因为已经使用注解方式了增加 <context:component-scan

结合项目(Spring+(基于注解的)SpringMVC和Mybatis+uploadify文件上传)--poi解析Excel文件

poi解析Excel文件 1.上传文件至服务器 2.解析Excel文件并返回数据集合 3.将数据保存到服务器 框架======Spring+(基于注解的)SpringMVC和Mybatis===== 第一步: 前台: jsp文件采用的是uploadify <div id="fileQueue"></div> <input type="file" id="brandFile"> js: <script ty

SpringMVC——拦截器及文件上传和下载

目录 自定义拦截器 拦截器类 在springmvc的配置文件中配置拦截器 文件上传 导入jar包 配置bean:multipartResolver 采用file.Transto 来保存上传的文件 文件下载 SpringMVC的处理器拦截器类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理.开发者可以自己定义一些拦截器来实现特定的功能. 过滤器与拦截器的区别:拦截器是AOP思想的具体应用. 过滤器: servlet规范中的一部分,任何java web工程都可以使用 在u

SpringMVC基础(二)_文件上传、异常处理、拦截器

实现文件上传 实现文件上传,需要借助以下两个第三方 jar 包对上传的二进制文件进行解析: commons-fileupload commons-io form表单的 enctype 取值必须为:multipart/form-data(默认为:application/x-www-form-urlencoded):enctype为表单请求正文的类型:method 属性必须取值为 post 方式:提供一个文件选择域: <input type="file"/> : <for

springMVC easyUI filebox 单个文件上传

被这个文件上传坑到现在,还是自己技术问题,照着之前extjs项目那边的上传实例,愣是上传不了 到后面就查了下springMVC的文件上传,按照那样搞定了http://blog.csdn.net/jadyer/article/details/7575934 easyui 前端文件 <form id="brandAddForm" method="post" enctype="multipart/form-data"> <table

SpringMVC 学习笔记(八)文件上传

Spring MVC 为文件上传提供了直接的支持,这种支持是通过即插即用的 MultipartResolver 实现的.Spring 用Jakarta Commons FileUpload 技术实现了一个MultipartResolver 实现类:CommonsMultipartResovler Spring MVC 上下文中默认没有装配 MultipartResovler,因此默认情况下不能处理文件的上传工作,如果想使用 Spring的文件上传功能,需现在上下文中配置 MultipartRes