文件上传—SSM框架文件上传
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<form action="index/newadd" method="post" enctype="multipart/form-data">
<table class="form">
<tr>
<td class="field">证件照:</td>
<td><input type="file" id="wkpicpath" class="text" name="attachs" /> </td>
<td class="field"id="hderrorinfo" style="margin-left:0px">${sessionScope.uploadwkError}</td>
</tr>
<tr>
<td class="field">头像:</td>
<td><input type="file" id="hdpicpath" class="text" name="attachs" /></td>
<td class="field"id="hderrorinfo" style="margin-left:0px">${sessionScope.uploadhdError}</td>
</tr>
<tr>
<td></td>
<td><label class="ui-blue"><input type="submit" name="submit" value="添加" /></label></td>
</tr>
</table>
</form>
/*
* 管理员新增用户
*/
@RequestMapping(value="/newadd",method=RequestMethod.POST)
public String doAddUser(HttpSession session,HttpServletRequest request,
@RequestParam(value="attachs",required=false)MultipartFile[] attachs,
User user){ //定义两个上传文件的路径
String wkpicpath = null;
String hdpicpath = null;
String errorinfo = null;
//定义上传过程管理标记
boolean flag = true;
//定义文件保存的位置
String path = request.getSession().getServletContext().getRealPath("statics"+File.separator+"uploadfiles");
//循环读取文件信息
for(int i=0;i<attachs.length;i++){
MultipartFile attach = attachs[i];
//判断文件是否为空
if(!attach.isEmpty()){
//判断是第几个文件
if(i==0){
errorinfo = "uploadwkError";
}else if(i==1){
errorinfo = "uploadhdError";
}
//获取源文件名
String oldName= attach.getOriginalFilename();
//获取源文件名后缀
String prefixName = FilenameUtils.getExtension(oldName); int fileSize = 500000;
//判断上传大小不得超过500K
if(attach.getSize()>fileSize){
session.setAttribute(errorinfo, "上传文件不得大于500k");
flag = false;
}else if(prefixName.equalsIgnoreCase("jpg")
|| prefixName.equalsIgnoreCase("png")
|| prefixName.equalsIgnoreCase("jpeg")
|| prefixName.equalsIgnoreCase("pneg")){
//判断上传格式
//定义新的文件名,当前系统时间+随机数+固定后缀,
//RandomUtils需要引入jar文件commons-lang.jar
//String fileName = System.currentTimeMillis()+RandomUtils.nextInt(1000000)+"personer.jpg";
String fileName = System.currentTimeMillis()+"personer.jpg";
//创建新的文件,用于接收用户上传的文件流
File targetFile = new File(path, fileName);
if(!targetFile.exists()){
targetFile.mkdirs();
} //将上传的文件保存
try {
attach.transferTo(targetFile);
} catch (Exception e) {
e.printStackTrace();
session.setAttribute(errorinfo,"上传失败!");
flag = false;
} //更新上传的路径
if(i==0){
wkpicpath = path + File.separator + fileName;
}else if(i==1){
hdpicpath = path + File.separator + fileName;
}
}else{
session.setAttribute(errorinfo,"图片格式不正确!");
flag = false;
}
}
} //准备User
if(flag){
user.setWkpicpath(wkpicpath);
user.setHdpicpath(hdpicpath);
user.setUserrole(1);
//插入数据库
if(userService.findAddUser(user)>0){
return "redirect:/index/user";
}
}
return "manager/user-add.jsp";
}
文件上传—SSM框架文件上传的更多相关文章
- SSH,SSM框架文件上传
一.了解文件上传 1.1 什么是文件上传 将本地文件通过流的形式写到服务器上 1.2 文件上传的技术 JspSmartUpload: 其组件是应用jsp进行B/S程序开发过 ...
- ssm框架文件上传
有两种方法 导包和上传配置自己搞: 第一种: 上传单个文件: @RequestMapping("/addfile1") public String addfile(@Request ...
- 文件上传—SSH框架文件上传
1.准备上传的api组件 <dependency> <groupId>commons-io</groupId> <artifactId>commons- ...
- 使用IntelliJ IDEA创建Maven聚合工程、创建resources文件夹、ssm框架整合、项目运行一体化
一.创建一个空的项目作为存放整个项目的路径 1.选择 File——>new——>Project ——>Empty Project 2.WorkspaceforTest为项目存放文件夹 ...
- 【转载】使用IntelliJ IDEA创建Maven聚合工程、创建resources文件夹、ssm框架整合、项目运行一体化
一.创建一个空的项目作为存放整个项目的路径 1.选择 File——>new——>Project ——>Empty Project 2.WorkspaceforTest为项目存放文件夹 ...
- ssm框架搭建(上)
前言 之前也说过,工作做的开发都是基于公司现有的框架,心里很没底.所以一直想自己能够搭建出ssm框架.经过多次尝试,终于成功了.这边文章将从两个方面进行,一是框架搭建,二是简单的增删查改. 正文 1. ...
- SSM框架文件远程服务器下载
1.首先你必须要建立连接 获取URL的输入流 2.之后就是文件读取和写入了 3.还有就是设置响应头,响应码等 代码 @RequestMapping("/fileDownLoad") ...
- ssm框架文件配置
1 简介 Spring MVC (web level),采取 MVC 架构,意图取代麻烦的 Servlet 写法,简化 web 层 MyBatis (dao level),意图取代 jdbc 操作数据 ...
- springmvc文件上传下载简单实现案例(ssm框架使用)
springmvc文件上传下载实现起来非常简单,此springmvc上传下载案例适合已经搭建好的ssm框架(spring+springmvc+mybatis)使用,ssm框架项目的搭建我相信你们已经搭 ...
随机推荐
- Ext3.4-EXT之嵌套表格的实现
其中使用到的"RowExpander.js"为extjs官方示例中自带的. 实现这个嵌套表格要注意两点技巧: 1 提供给外层表格的dataStore的数据源以嵌套数组的形式表示细节 ...
- 服务端用例设计的思(tao)路!
服务端的测试简单来说就是除了前端以外的的测试. 总的来说可以分为以下两类: 1. WEB或者APP的提供业务逻辑的服务端接口测试 2. 数据库.缓存系统.中间件..jar包依赖.输入输 ...
- EOF ---shell编程
转自:http://blog.163.com/njut_wangjian/blog/static/1657964252013112152418345/ 在shell编程中,”EOF“通常与”<& ...
- json 数据分析
/* 健一健康头条 */ try { String url = "http://www.j1health.com/j1api.php/index/getJ1healthHotLists&qu ...
- Android使用百度定位API时获取的地址信息为null
option.setAddrType("all"); //加上这个配置后才可以取到详细地址信息
- Set集合遍历方式
for(String str : set) { System.out.println(str); } for (Iterator iter = set.iterator(); iter.hasNext ...
- Thinkphp 图形验证码无法显示
不显示验证码的代码: public function verify(){ $verify = new \Think\Verify(); $verify->entry(); } 修改为: publ ...
- 英语——'s和s'和s的区别
举个例子吧,student's 是表示学生的,名词单数形式的所有格students' 是表示学生们的,名词复数形式的所有格students 是表示学生们,名词的复数形式
- getOwnPropertyNames() & keys()
1.getOwnPropertyNames方法可以获得对象的所有属性名,并储存于一个数组当中. keys方法只能获取可遍历的属性名并储存于数组. 2.在完成notepad模块模拟的过程中使用了getO ...
- Dropwizard简单入门
Dropwizard:一个简洁的RESTful Web框架 Dropwizard跨越了开发库与框架的界限,旨在为Web应用所需的功能提供高性能.可靠的实现.Dropwizard将这些功能抽象为可重用的 ...