ssm框架下上传图片及其他信息
先引入这两个包:
<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框架下上传图片及其他信息的更多相关文章
- 关于在SSM框架下使用PageHelper
首先,如果各位在这块配置和代码有什么问题欢迎说出来,我也会尽自己最大的能力帮大家解答 这些代码我都是写在一个小项目里的,项目的github地址为:https://github.com/Albert-B ...
- ssm框架下怎么批量删除数据?
ssm框架下批量删除怎么删除? 1.单击删除按钮选中选项后,跳转到js函数,由函数处理 2. 主要就是前端的操作 js 操作(如何全选?如何把选中的数据传到Controller中) 3.fun()函数 ...
- Jquery DataTable AJAX跨域请求的解决方法及SSM框架下服务器端返回JSON格式数据的解决方法
如题,用HBuilder开发APP,涉及到用AJAX跨域请求后台数据,刚接触,费了不少时间.幸得高手指点,得以解决. APP需要用TABLE来显示数据,因此采用了JQ 的DataTable. 在实现 ...
- SSM框架下分页的实现(封装page.java和List<?>)
之前写过一篇博客 java分页的实现(后台工具类和前台jsp页面),介绍了分页的原理. 今天整合了Spring和SpringMVC和MyBatis,做了增删改查和分页,之前的逻辑都写在了Servle ...
- ssm框架下实现文件上传
1.由于ssm框架是使用Maven进行管理的,文件上传所需要的jar包利用pom.xml进行添加,如下所示: <properties> <commons-fileupload.v ...
- SSM框架下 Failed to load resource: the server responded with a status of 404 (Not Found)错误
这个错误提示的是js的引用路径有错: 1.检查应用路径是否正确(我的问题是路径是正确的但是去到页面就会提示404错误) 引用路径,最好都使用绝对路径 <script type="tex ...
- SSM框架下的redis缓存
基本SSM框架搭建:http://www.cnblogs.com/fuchuanzhipan1209/p/6274358.html 配置文件部分: 第一步:加入jar包 pom.xml <!-- ...
- SSM框架下结合 log4j、slf4j打印日志
首先加入log4j和slf4j的jar包 <!-- 日志处理 <!-- slf4j日志包--> <dependency> <groupId>org.slf4j ...
- ssm框架下的文件上传和文件下载
最近在做一个ssm的项目,遇到了添加附件和下载的功能,在网上查了很多资料,发现很多都不好用,经过摸索,发现了一套简便的方法,和大家分享一下. 1.在自己已经构建好的maven web项目中 pom. ...
随机推荐
- 数据库-mongodb-索引
1.索引提高查询速度,降低写入速度,权衡常用的查询字段,不必在太多列上建立索引 2.在mongodb中,索引可以按字段升序.降序来创建,便于排序 3.默认是使用btree 来组织索引文件,2.4版以后 ...
- 由free命令想到的
root@xdj-Z9PA-D8-Series:~# free -m total used free shared buffers cached Mem: 15977 1683 14293 0 132 ...
- BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第11章节--为Office和SP解决方式开发集成Apps 集成SP和Office App
BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第11章节--为Office和SP解决方式开发集成Apps 集成SP和Office App 你能够用两种 ...
- Hadoop高速入门
Hadoop高速入门 先决条件 支持平台 GNU/Linux是产品开发和执行的平台. Hadoop已在有2000个节点的GNU/Linux主机组成的集群系统上得到验证. Win32平台是作为开发平台支 ...
- python的range()函数使用方法
python的range()函数使用非常方便.它能返回一系列连续添加的整数,它的工作方式类似于分片.能够生成一个列表对象. range函数大多数时常出如今for循环中.在for循环中可做为索引使用.事 ...
- Swift - 获取应用名称、应用版本、设备型号、系统版本等信息
有时我们在 App 中提交一些统计信息或者用户反馈信息时,为了能更好地进行分析,通常会附带上当前应用程序的名称.版本号.设备型号.以及设备系统版本.下面演示如何获取这些信息. 1,效果图 程序启动后自 ...
- Oracle 11G R2 RAC中的scan ip 的用途和基本原理
Oracle 11G R2 RAC增加了scan ip功能,在11.2之前,client链接数据库的时候要用vip,假如你的cluster有4个节点,那么客户端的tnsnames.ora中就对应有四个 ...
- System.setProperty 与 System.getProperty
转自:https://www.cnblogs.com/woftlcj/p/8404451.html System可以有对标准输入,标准输出,错误输出流:对外部定义的属性和环境变量的访问:加载文件和库的 ...
- Google浏览器vim命令
使用鼠标久了,手腕.肩膀依旧疼痛.偶尔逛知乎,看到有人推荐chrome浏览器的vimium插件(火狐浏览器是vimperator),安装了使用了几天,真不愧是浏览器神器,好用到想哭,而且非常容易上手. ...
- flask之jinji2模板介绍
1.1.模板传参 (1)主程序 from flask import Flask,render_template app = Flask(__name__) @app.route('/') def ...