先引入這兩個包:javascript
<dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>1.3.1</version> </dependency>
html:html
注意:enctype="multipart/form-data" 這個要這樣定義java
<form id="form-register" action="${pageContext.request.contextPath}/user/upLoadPicture.do" method="post" enctype="multipart/form-data" onsubmit="return check()" class="form-horizontal m-t"> <div class="form-group"> <label class="col-sm-5 control-label">上傳頭像:</label> <input type="file" name="file" id="file" style="height:30px;background-color:#ff9900;outline:none;border:none;width:10%;" onchange="uploadPic()"> </div> <div class="form-group"> <label class="col-sm-5 control-label">頭像顯示:</label> <div class="col-sm-4" > <img border="0" width="40" height="50" src="${user.picture}"> </div> </div> <input type="hidden" name="id" id="id" readonly="readonly" value="${user.id}" /> <div class="form-group"> <label class="col-sm-5 control-label"><span style="color: red;">*</span>用戶名:</label> <div class="col-sm-4" > <input type="text" id="name" name="name" class="form-control" placeholder="請輸入用戶名" onblur="checkName()" maxlength="16" value="${user.name}"> <div id="user_prompt">用戶名由4到16位(字母,數字,下劃線,減號)</div> </div> </div> <div class="form-group"> <label class="col-sm-5 control-label">真實姓名:</label> <div class="col-sm-4"> <input type="text" id="realName" name="realName" class="form-control" placeholder="請輸入真實姓名" value="${user.realName}"> </div> </div> <div class="form-group"> <label class="col-sm-5 control-label">性別</label> <div class="col-sm-4"> <c:if test="${user.gender=='男'}"> <div class="radio-box" style="float:left"> <input name="gender" type="radio" id="male" value="男" checked> <label for="sex-1">男</label> </div> <div class="radio-box" style="float:left"> <input name="gender" type="radio" id="female" value="女"> <label for="sex-2">女</label> </div> </c:if> <c:if test="${user.gender=='女'}"> <div class="radio-box" style="float:left"> <input name="gender" type="radio" id="male" value="男"> <label for="sex-1">男</label> </div> <div class="radio-box" style="float:left"> <input name="gender" type="radio" id="female" value="女" checked> <label for="sex-2">女</label> </div> </c:if> </div> </div> <div class="form-group"> <label class="col-sm-5 control-label">民族</label> <div class="col-sm-4"> <select name="ethGro" class="form-control input-sm"> <option selected="selected" value="${user.ethGro}">${user.ethGro}</option> </select> </div> </div> <br> <div class="form-group" align=center> <div class="col-sm-8 col-sm-offset-2"> <button class="btn btn-primary" type="submit">保存</button> </div> </div> </form
後臺:sql
@RequestMapping(value="upLoadPicture.do",method = RequestMethod.POST) public String upload(User user,HttpServletRequest request,ModelMap map) throws Exception{ System.out.println(request.getParameter("name")); //保存數據庫的路徑 String sqlPath = null; //定義文件保存的本地路徑 String localPath="D:\\File\\"; //建立文件 File dir=new File(localPath); if(!dir.exists()){ dir.mkdirs(); } //定義 文件名 String filename=request.getParameter("name").toString(); if(!user.getFile().isEmpty()){ //生成uuid做爲文件名稱 String uuid = UUID.randomUUID().toString().replaceAll("-",""); //得到文件類型(能夠判斷若是不是圖片,禁止上傳) String contentType=user.getFile().getContentType(); //得到文件後綴名 String suffixName=contentType.substring(contentType.indexOf("/")+1); //獲得 文件名 filename=uuid+"."+suffixName; System.out.println(filename); //文件保存路徑 user.getFile().transferTo(new File(localPath+filename)); sqlPath = "/images/"+filename; System.out.println(sqlPath); user.setPicture(sqlPath); userService.updateUser(user); } //把圖片的相對路徑保存至數據庫 userService.updateUserNoPicture(user); Page page=new Page(); page.setCurrentPage(1); List<User> users = userService.getAllUser(page); page.setRows( userService.count()); map.addAttribute("page",page); map.addAttribute("users",users); return "user_list"; }