@RequestMapping(value="/addIdcardsSubmit",method={RequestMethod.POST,RequestMethod.GET}) public String addIdcardsSubmit(HttpServletRequest request,Idcard idcard,MultipartFile idcard_pic) throws IllegalStateException, IOException{ String originalFileName = idcard_pic.getOriginalFilename(); System.out.println("originalFileName"+originalFileName); String idcardCode = idcard.getIdcardcode(); //改名字 String pic_path = "F:\\javaEEWorkspace\\DemoJimSpringMVCMyBatis\\src\\main\\webapp\\pic\\";//字符串需要两个斜杠 String NewFileName = UUID.randomUUID()+originalFileName.substring(originalFileName.lastIndexOf("."));//修改照片名字 //新的图片 File newfile = new File(pic_path + NewFileName); //将内存中的数据写入磁盘 idcard_pic.transferTo(newfile); idcard.setPic(NewFileName); System.out.println("idcardCode"+idcardCode); int num = idcardService.addIdCard(idcard); if(num == 1) return "/success"; else return "/fail"; }
下面是前端代码:
//index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" isELIgnored="false" pageEncoding="utf-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!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>Insert title here</title> </head> <body> ${pageContext.request.contextPath} <a href="${pageContext.request.contextPath}/addIdcard.action">增加用户信息</a> <form action="${pageContext.request.contextPath}/findIdcardByIdcardcode.action" name="myform" method="post"> 查询条件: <table width="60%" border=1> <tr> <td width="50%"><input type="text" name="idcardcode" /></td> <td><input type="submit" value="查询"/></td> </tr> </table> </form> <form action="${pageContext.request.contextPath}/deleteIdcardAll.action" name="myform1" method="post"> 身份证信息列表: <table width="60%" border=1> <tr> <td colspan="3"><input type="submit" value="批量删除" /></td> </tr> <tr> <td>身份证</td> <td>身份证图片</td> <td>操作</td> </tr> <c:forEach items="${idcardsAll}" var="idcard" varStatus="status"><!-- 遍历idcardList集合,然后一个一个给idcard,idcard就 相当于是Idcard--> <tr height="100"> <td width="200"> <input type="checkbox" name="ids" value="${idcard.id }"/> <input type="hidden" name="idcardList[${status.index }].id" value="${idcard.id }"/> ${idcard.idcardcode} </td> <td width="200"> <c:if test="${idcard.pic !=null}"> <img src="./pic/${idcard.pic}" alt="" width="100" height="100"/> <br/> </c:if> </td> <td> <a href="${pageContext.request.contextPath }/findIdcardById.action?id=${idcard.id}">查询</a> <a href="${pageContext.request.contextPath }/editItems.action?id=${idcard.id}">修改</a> <a href="${pageContext.request.contextPath }/deleteIdcardById.action?id=${idcard.id}">删除</a> </td> </tr> </c:forEach> </table> </form> </body> </html>
//findIdcard.jsp
<%@ 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> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> ${idcard.idcardcode} <form id="idcardForm" action="${pageContext.request.contextPath }/findIdcardsAll.action" method="post"> 身份证信息: <table width="100%" border=1> <tr> <td>身份证信息</td> <td><input type="text" name="idcardcode" disabled="disabled" value="${idcard.idcardcode}"/></td> </tr> <tr> <td>身份证图片</td> <td> <c:if test="${idcard.pic !=null}"> <img src="./pic/${idcard.pic}" width=100 height=100/> <br/> </c:if> </td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="返回"/> </td> </tr> </table> </form> </body> </html>
//addIdcard.jsp
<%@ 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> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form id="idcardForm" action="${pageContext.request.contextPath }/addIdcardsSubmit.action" method="post" enctype="multipart/form-data"> 身份证信息: <table width="100%" border=1> <tr> <td>身份证信息</td> <td><input type="text" name="idcardcode"/></td> </tr> <tr> <td>身份证图片</td> <td> <input type="file" name="idcard_pic" > <br/> </td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="添加"/> </td> </tr> </table> </form> </body> </html>