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. ...
随机推荐
- yii 表单小部件使用
首先创建model层 因为要使用表单小部件 所以要加载相应的组件 这里需要的组件有 yii\widgets\ActiveForm 和 yii\helpers\Html 接下来在model定义的clas ...
- RGB 与 (RGB转 YCbCr再转为 RGB)的图像
RGB 与 (RGB转 YCbCr再转为 RGB)的图像 不可逆,能够从 矩阵的逆运算看出来. 附上 matlab 代码: clc,clear; Source=imr ...
- 并发编程网 - ifeve.com
并发编程网 - ifeve.com 让天下没有难学的技术 首页 JAVA 深入浅出ClassLoader 深入浅出ClassLoader Dedicate to Molly. 你真的了解ClassLo ...
- HTML5开发移动web应用——Sencha Touch篇(8)
DataView是Sencha Touch中最重要的组件,用于数据的可视化.数据可视化的重要性不言而喻,能够讲不论什么数据以形象的方式展示给用户. 眼下,怎样更好地可视化是很多公司或框架都在追求的. ...
- Android自己定义View之组合控件 ---- LED数字时钟
先上图 LEDView效果如图所看到的. 之前看到一篇博客使用两个TextView实现了该效果.于是我想用自己定义控件的方式实现一个LEDView.使用时就可以直接使用该控件. 採用组合控件的方式,将 ...
- tp中使用事务
是什么 事务是为了防止,多个操作,其中有失败,数据有部分被执行成功的时候使用的. 比如,银行,用户转账.张三钱扣了,结果李四钱还增加! 这个时候需要使用事务,确保张三钱扣了,李四的钱也增加,才真正的成 ...
- HTML iframe 和 frameset 的区别
转自:http://www.cnblogs.com/polk6/archive/2013/05/24/3097430.html HTML iframe 和 frameset 的区别 iframe 和 ...
- Core篇——初探依赖注入
目录 1.DI&&IOC简单介绍 2.UML类图中六种关联关系 3..net core 中DI的使用 4..net core DI初始化源码初窥 DI&&IOC简单介绍 ...
- Ztree自动触发第一个节点的点击事件
1.代码 $(function () { var setting = { //check属性放在data属性之后,复选框不起作用 //check: { // enable: true //}, dat ...
- Codeforces 993A. Two Squares(暴力求解)
解题思路(暴力解法) 平行于x轴的正方形和与x轴成45度倾斜的正方形相交的点中必定有整数点.即若两正方形相交,必定存在整数i,j,使(i,j)同时属于两个正方形. 我们把两个正方形中的整数点都找出来, ...