先引入这两个包:

                <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:

注意:enctype="multipart/form-data"  这个要这样定义

<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

后台:

@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";
}

ssm框架下上传图片及其他信息的更多相关文章

  1. 关于在SSM框架下使用PageHelper

    首先,如果各位在这块配置和代码有什么问题欢迎说出来,我也会尽自己最大的能力帮大家解答 这些代码我都是写在一个小项目里的,项目的github地址为:https://github.com/Albert-B ...

  2. ssm框架下怎么批量删除数据?

    ssm框架下批量删除怎么删除? 1.单击删除按钮选中选项后,跳转到js函数,由函数处理 2. 主要就是前端的操作 js 操作(如何全选?如何把选中的数据传到Controller中) 3.fun()函数 ...

  3. Jquery DataTable AJAX跨域请求的解决方法及SSM框架下服务器端返回JSON格式数据的解决方法

    如题,用HBuilder开发APP,涉及到用AJAX跨域请求后台数据,刚接触,费了不少时间.幸得高手指点,得以解决. APP需要用TABLE来显示数据,因此采用了JQ 的DataTable.  在实现 ...

  4. SSM框架下分页的实现(封装page.java和List<?>)

    之前写过一篇博客  java分页的实现(后台工具类和前台jsp页面),介绍了分页的原理. 今天整合了Spring和SpringMVC和MyBatis,做了增删改查和分页,之前的逻辑都写在了Servle ...

  5. ssm框架下实现文件上传

      1.由于ssm框架是使用Maven进行管理的,文件上传所需要的jar包利用pom.xml进行添加,如下所示: <properties> <commons-fileupload.v ...

  6. SSM框架下 Failed to load resource: the server responded with a status of 404 (Not Found)错误

    这个错误提示的是js的引用路径有错: 1.检查应用路径是否正确(我的问题是路径是正确的但是去到页面就会提示404错误) 引用路径,最好都使用绝对路径 <script type="tex ...

  7. SSM框架下的redis缓存

    基本SSM框架搭建:http://www.cnblogs.com/fuchuanzhipan1209/p/6274358.html 配置文件部分: 第一步:加入jar包 pom.xml <!-- ...

  8. SSM框架下结合 log4j、slf4j打印日志

    首先加入log4j和slf4j的jar包 <!-- 日志处理 <!-- slf4j日志包--> <dependency> <groupId>org.slf4j ...

  9. ssm框架下的文件上传和文件下载

    最近在做一个ssm的项目,遇到了添加附件和下载的功能,在网上查了很多资料,发现很多都不好用,经过摸索,发现了一套简便的方法,和大家分享一下. 1.在自己已经构建好的maven  web项目中 pom. ...

随机推荐

  1. Oracle-定时任务

    PLSQL->新建->命令行窗口 --存储过程 create or replace procedure prd_remove_error_data AS BEGIN UPDATE rpt_ ...

  2. hdoj--1201--18岁生日(模拟)

    18岁生日 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  3. hdoj--1251--统计难题(字典树)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others) Total Subm ...

  4. excel如何将一列按奇偶数分成两列

    借助于函数.上图说明一切: 方法一.OFFSET函数, 奇数列公式:C1=OFFSET($A$1,ROW()*2-2,), 偶数列公式:D1=OFFSET($A$1,ROW()*2-1,) 一起下拉即 ...

  5. 检测Nginx访问成功(状态码200)的IP及次数

    cat  access.log |awk '{print $1,$9}'|grep '200'|sort | uniq -c|awk '{print $2" "$1}'

  6. Ubuntu14.04下初步使用MongoDB

    不多说,直接上干货! Ubuntu14.04下Mongodb(在线安装方式|apt-get)安装部署步骤(图文详解)(博主推荐) shell命令模式 输入mongo进入shell命令模式,默认连接的数 ...

  7. git工具的安装和使用

    啰嗦几句: 世界上本没有后悔药,但软件开发提供了后悔药,那就是代码管理工具.它可以让你的代码穿越回以前的状态,甚至可以指定某一个时刻,而且还可以穿越回来. 当下流行的代码管理工具有 SVN 和 GIT ...

  8. LinrFont UWP 字体预览工具下载

    Windows 10 用户 购买 https://www.microsoft.com/zh-cn/p/linrfont/9nkh5mlvt819

  9. web工程中的各种路径(eclipse开发)

    目前遇到的 web 工程中要写url和路径的文件有 webContent中.jsp/.html :action src中的servlet类 : 映射地址.重定向.请求转发.访问资源文件(webCont ...

  10. Windos下的6种IO模型简要介绍

    windows进行数据的收发有6种IO模型.分别是阻塞(blocking)模型,选择(select)模型,异步选择(WSAAsyncSelect)模型,事件选择(WSAEventSelect )模型, ...