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. ...
随机推荐
- CF864A Fair Game
CF864A Fair Game 题意翻译 CF864A Fair Game 题意: Petya和Vasya决定玩一个游戏,他们有偶数张卡片,每张卡片上一个数字.每人选择一个数字(两个人选择的数字不能 ...
- 两列等高布局 padding+margin的负值 CSS布局奇淫技巧之-多列等高
代码: 效果图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/ ...
- 假设让我又一次设计一款Android App
转载请注明出处: 本文来自aspook的博客:http://blog.csdn.net/ahence/article/details/47154419 开发工具的选择 开发工具我将选用Android ...
- 自己动手写CPU之第七阶段(6)——乘累加指令实现思路
将陆续上传本人写的新书<自己动手写CPU>.今天是第29篇.我尽量每周四篇 亚马逊的销售地址例如以下,欢迎大家围观呵! http://www.amazon.cn/dp/b00mqkrlg8 ...
- angularjs1-3,$apply,$watch
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- Android持久化保存cookie
在解析网页信息的时候,需要登录后才能访问,所以使用httpclient模拟登录,然后把cookie保存下来,以供下一次访问使用,这时就需要持久化cookie中的内容. 在之前先科普一下基础知识: 什么 ...
- 杂项:ExtJS
ylbtech-杂项:ExtJS extjs是一种软件.自动生成行号,支持checkbox全选,动态选择显示哪些列,支持本地以及远程分页,可以对单元格按照自己的想法进行渲染,这些也算可以想到的功能. ...
- WEBSERVICE之JDK开发webservice
转自:https://www.cnblogs.com/w-essay/p/7357262.html 一.开发工具与环境 1. jdk1.6版本以上(jdk1.6.0_21及以上版本) 2 .eclip ...
- AJAX请求 $.ajaxSetup方法的使用
转自:https://blog.csdn.net/qq_23476319/article/details/78798885 jQuery.ajaxSetup()函数用于设置AJAX的全局默认设置. 该 ...
- 自动刷github提交记录
前言 进入自己github主页会看到自己的提交记录,如果某天没有提交记录,那天的小方框就显示灰色.强迫症的我,每次进来看着就感觉不爽, 想着自己每天记得提交点东西,争取像阮一峰大神一样,每天都有提交记 ...