java 实现图片上传功能
1:jsp 页面上传图片按钮在这里我就写相关的代码
<div class="control-group">
<label class="control-label">营业执照:</label>
<div class="controls">
<input type="file" class="form-control-input" name="yingYeZhiZhaoName" value="${office.yingYeZhiZhao }"/>
<%-- <input type="hidden" name="yingYeZhiZhao" value="${office.yingYeZhiZhao }"/> --%>
</div>
<div class="controls"><img src="${office.yingYeZhiZhao }" width="203" title="点击更换图片" id="logoImgTag" /></div>
</div>
2:下面是控制层action 代码
@RequestMapping("/add")
public String addOffice(HttpServletRequest request, HttpServletResponse response,Office office, Model model, RedirectAttributes redirectAttributes, User user,String officeId,String userId,String roleName,MultipartFile yingYeZhiZhaoName) {
String originalFilename = yingYeZhiZhaoName.getOriginalFilename();
if((officeId !=null && !"".equals(officeId))&&(userId !=null && !"".equals(userId)) ){//修改用户信息
User newUser = systemService.getUser(userId);
Office newOffice = officeService.get(officeId); //用户表基本信息
if(StringUtils.isNotBlank(user.getPassword())){
String password = SystemService.entryptPassword(user.getPassword());
newUser.setPassword(password);
}
//newUser.setName(user.getName());//公司名称 //公司表信息
newOffice.setMaster(office.getMaster());
newOffice.setMasterSex(office.getMasterSex());
newOffice.setZhiWei(office.getZhiWei());
newOffice.setEmail(office.getEmail());//公司邮箱
newOffice.setName(office.getName());//公司名称
newOffice.setJianCheng(office.getJianCheng());//公司简称
newOffice.setProvince(office.getProvince());//省
newOffice.setCity(office.getCity());//市
newOffice.setAddress(office.getAddress());
newOffice.setDuiGongZhangHu(office.getDuiGongZhangHu());//对公账户
newOffice.setDuiGongShouKuan(office.getDuiGongShouKuan());//对公收款
newOffice.setDuiGongYinHang(office.getDuiGongYinHang());//对公银行
newOffice.setQuHao(office.getQuHao());
newOffice.setTelephone(office.getTelephone());//公司固话
newOffice.setRemarks(office.getRemarks());//备注 if(originalFilename !=null && !"".equals(originalFilename)){//如果是重新上传了图片
int num=(int)(Math.random()*100000);
Map<String, String> map = QiniuUtils.fileUploadPdf(yingYeZhiZhaoName,num+officeId);
newOffice.setYingYeZhiZhao(map.get(Constants.QIUNIU_EXHI_IMAGE_SOURCE));
}
officeService.updateSysOffice(newOffice);
systemService.udateUser(newUser);
//officeService.save(newOffice); }else{//添加用户信息 if(originalFilename !=null && !"".equals(originalFilename)){//如果是重新上传了图片
int num=(int)(Math.random()*100000);
Map<String, String> map = QiniuUtils.fileUploadPdf(yingYeZhiZhaoName,num+officeId);
office.setYingYeZhiZhao(map.get(Constants.QIUNIU_EXHI_IMAGE_SOURCE));
}
List<Role> roles = new ArrayList<Role>(0);
Role role = null;
User newUser = new User();
newUser.setPassword(SystemService.entryptPassword(user.getPassword()));// 设置密码
newUser.setMobile(user.getMobile());// 手机号码
newUser.setLoginName(user.getMobile());// 登录名(手机或者邮箱) role = systemService.getRole(roleName);//这地方判断是2是会展代理商15是会展主办方
roles.add(role); Role admin = new Role();
admin.setId(Constants.HE_ZUO_SHANG_ADMIN);// 默认注册的用户就是合作商管理员
roles.add(admin);
newUser.setRoleList(roles); Area area = new Area();
area.setId("1");
office.setCode(UUID.randomUUID().toString());
office.setType("1");
office.setGrade("1");
office.setUseable("1");
office.setDelFlag("0");
User createBy = new User();
createBy.setId("1");
Office poffice = new Office();
poffice.setId("0");
office.setParent(poffice);
office.setParentIds("0,");
office.setArea(area);
office.preInsert();
office.setIsNewRecord(true);
office.setCreateBy(createBy);
office.setUpdateBy(createBy);
office.setRole(role);
officeService.register(office);
newUser.setOffice(office);
newUser.setCompany(office);
newUser.setName(office.getName());
systemService.saveUser(newUser); } String id = "0".equals(office.getParentId()) ? "" : office.getParentId();
return "redirect:" + adminPath + "/sys/office/list?id=" + id + "&parentIds=" + office.getParentIds();
}
以上实现图片功能是把图片上传到七牛服务器上
注意
<input type="file" class="form-control-input" name="yingYeZhiZhaoName" value="${office.yingYeZhiZhao }"/> 控件里面的name 不要和提交时表单对象里面的属性一样,所以这地方最好不要写对象属性的名字
3:下面是七牛部分代码
/**
* 七牛招展函上传
* @param file
* @param name
* @return
*/
public static Map<String, String> fileUploadPdf(MultipartFile file, String name) {
Map<String, String> map = new HashMap<String, String>();
try {
UploadManager uploadManager = new UploadManager(); // 上传到云中的文件名
// String newName = name+System.currentTimeMillis(); byte[] bytes = file.getBytes(); uploadManager.put(bytes, name, getUpToken0(name));
String isProduct = Global.getConfig("isProduct");
String url = null;
if ("Y".equalsIgnoreCase(isProduct)) {
url = Constants.QIUNIU_EXHI_IMAGE_URL;
} else if ("N".equalsIgnoreCase(isProduct)) {
url = Constants.QIUNIU_TEST_IMAGE_URL;
}
map.put("fileUrl", url + name);
map.put("disposeUrl", url + name + ":result");
} catch (Exception e) {
log.error("七牛上传文件失败:" + e.getMessage());
e.printStackTrace();
}
return map;
}
java 实现图片上传功能的更多相关文章
- java实现图片上传功能,并返回图片保存路径
1.前端html <div class="form-group"> <label for="inputPassword3" class ...
- thinkphp达到UploadFile.class.php图片上传功能
片上传在站点里是非经常常使用的功能.ThinkPHP里也有自带的图片上传类(UploadFile.class.php) 和图片模型类(Image.class.php).方便于我们去实现图片上传功能,以 ...
- Spring+SpringMVC+MyBatis+easyUI整合优化篇(七)图片上传功能
日常啰嗦 前一篇文章<Spring+SpringMVC+MyBatis+easyUI整合优化篇(六)easyUI与富文本编辑器UEditor整合>讲了富文本编辑器UEditor的整合与使用 ...
- java多图片上传--前端实现预览--图片压缩 、图片缩放,区域裁剪,水印,旋转,保持比例。
java多图片上传--前端实现预览 前端代码: https://pan.baidu.com/s/1cqKbmjBSXOhFX4HR1XGkyQ 解压后: java后台: <!--文件上传--&g ...
- PHP语言学习之php做图片上传功能
本文主要向大家介绍了PHP语言学习之php做图片上传功能,通过具体的内容向大家展示,希望对大家学习php语言有所帮助. 今天来做一个图片上传功能的插件,首先做一个html文件:text.php < ...
- [Ting's笔记Day8]活用套件carrierwave gem:(3)Deploy图片上传功能到Heroku网站
前情提要: 身为Ruby新手村民,创造稳定且持续的学习步调很重要,我用的方法就是一周在IT邦写三篇笔记,希望藉由把笔记和遇到的bug记录下来的过程,能帮助到未来想用Ruby on Rails架站的新手 ...
- H5 利用vue实现图片上传功能。
H5的上传图片如何实现呢? 以下是我用vue实现的图片上传功能,仅供参考. <!DOCTYPE html> <html> <head> <meta chars ...
- 给DEDECMS广告管理中增加图片上传功能
dedecms的广告管理功能稍微有点次,本文就是在dedecms广告管理原有的基础上增加广告图片上传功能. 安装方法,对应自己的dedecms版本下载对应的编码然后解压把里面的文件放在后台目录覆盖即可 ...
- vue 图片上传功能
这次做了vue页面的图片上传功能,不带裁剪功能的! 首先是html代码,在input框上添加change事件,如下: <ul class="clearfix"> ...
随机推荐
- linux 下部署web 程序
linux 下部署web 程序 1. 创建ucenter用户 一般情况下,发布应用程序都不是使用root用户的,需要创建一个普通用户来发布程序: 创建ucenter用户: useradd -d /uc ...
- 在服务器CentOS7版本安装Nginx
简介 经常用使用Nginx来部署我们的网站,我的服务器是CentOS7.我不喜欢使用下载一个Nginx解压包然后解压的那种,我喜欢下面的这种. 安装 yum包管理工具是不带nginx,所以得先添加,在 ...
- selenium Python实现附件上传
对于web页面的上传功能一般有两类实现方式:一类是将本地文件的路径作为一个值放在input标签中,通过form表单将这个值提交给服务器:另一个类是插件上传,一般基于flash/javascript或者 ...
- MySQL双机热备环境搭建
一. 前期准备 准备两台服务器(电脑),接入到同一局域网中,能够使双方可以ping通: 安装MySQL数据库,具体安装方法网上很全面,但是安装的版本需保持一致: 服务器IP地址设置. l A服 ...
- 本地缓存Caffeine
Caffeine 说起Guava Cache,很多人都不会陌生,它是Google Guava工具包中的一个非常方便易用的本地化缓存实现,基于LRU算法实现,支持多种缓存过期策略.由于Guava的大量使 ...
- disconf---分布式配置管理平台的搭建(linux版本)
一.获取disconf的代码 地址:https://github.com/knightliao/disconf 二.搭建基础服务 ①redis,mysql,zookeeper在博主的其他博文中有相应搭 ...
- netty学习1
1.IO java 自带IO流 1)同步处理方式,一个请求占用一个线程,高并发常见出现问题 2.java nio 实在jdk1.4版本上新添加的new IO / 非阻塞IO 可多路并发 3.Netty ...
- 《JavaScript高级程序设计》读书笔记(三)基本概念第五小节流程控制语句
内容---语法 ---数据类型 上一小节---流程控制语句 本小节---理解函数 语句--ECMA-262规定了一组语句,也称流程控制语句 if语句-- 条件可以是任意表达式,-- ECMAScrip ...
- Java Web 笔记(杂)
Java Web 概述 什么是Java Web 在Sun的Java Servlet 规范中,对Java Web 应用做了这样的定义: "Java Web" 应用由一组Servlet ...
- Mobility Express部署外部镜像服务器
1.当我们部署完ME的时候,发现有一些AP虽然显示已经加入了WLC(ME),但是它其实并没有正常的工作,显示不可用: (Cisco Controller) >show ap su Number ...