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. ...
随机推荐
- 跳出$.each()循环
return false:将停止循环 ,跳出eachreturn true:跳至下一个循环(就像在普通的循环中使用'continue').
- File System Design Case Studies
SRC=http://www.cs.rutgers.edu/~pxk/416/notes/13-fs-studies.html Paul Krzyzanowski April 24, 2014 Int ...
- etymology-R
1)vor = to eat devour vt. 狼吞虎咽地吃光: 吞没,毁灭: 目不转睛地看[de-向下+vour-吃] voracity n.贪食,贪婪.拉丁词根vor-,vorac-表示吞食 ...
- [GraphQL] Mutations and Input Types
Sometimes, you want to resues object type when doing mutation, you can use 'input' type to help: inp ...
- C++类中静态变量和静态方法的注意事项
在C++中,静态成员是属于整个类的而不是某个对象,静态成员变量仅仅存储一份供全部对象共用.所以在全部对象中都能够共享它.使用静态成员变量实现多个对象之间的数据共享不会破坏隐藏的原则,保证了安全性还能够 ...
- OpenST Basic tool library
/***************************************************************************** * OpenST Basic tool l ...
- centos 下 KVM虚拟机的创建、管理与迁移
kvm虚拟机管理 一.环境 role hostname ip OS kvm_server target 192.168.32.40 ...
- UVA 10127- Ones 数学
Given any integer 0 ≤ n ≤ 10000 not divisibleby 2 or 5, some multiple of n is a number whichin decim ...
- angularjs1-6,自定义服务
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- dns tunnel工具地址