在做头像上传的过程中通常是将图片保存到数据库中,这里简单介绍一中将图片保存到数据库的方法:

jsp代码:

  <div>
<input class="avatar-input" id="avatarInput" name="avatar_file" type="file" />
<button class="btn btn-primary btn-block avatar-save" type="button" id="btn">完成</button>
</div>
//显示头像
<div>
<img id="image" src="${pageContext.request.contextPath}/Personal.do?method=Pictureshow" />
</div>

js代码:

   $("#avatarInput").wrap("<form id='myupload' action='${pageContext.request.contextPath}/Personal.do?method=Pictureupload' enctype='multipart/form-data' method='post' ></form>")
var formdata;
$("#avatarInput").change(function(){
formdata=new FormData($("#myupload")[0]);
})
$("#btn").click(function(){
    $.ajax({
       type: 'post',
url: '${pageContext.request.contextPath}/Personal.do?method=Pictureuplo',
data: formdata,
async:false,
cache:false,
contentType:false,
processData:false,
success: function(data) {
alert(data);
}
});
});

注:为了方便获取返回值,我这里选择用ajax传值,根据需求而定,也可以直接用form表单传值。

Controller(头像插入):

 @RequestMapping(params = "method=Pictureupload",headers="content-type=multipart/*" ,method=RequestMethod.POST)
public @ResponseBody int Pictureupload(@RequestParam("avatar_file")MultipartFile file,HttpServletRequest request)throws Exception{
Map map=new HashMap<>();
int YGID001 = (int)request.getSession().getAttribute("ygid001");
String name = file.getOriginalFilename();
long filesize = file.getSize();
//System.err.println(filesize);
if(filesize>200000){//判断图片大小
return -1;
}
//System.out.println(file.getBytes());
map.put("in", file.getBytes());
map.put("YGID001", YGID001);
return pcs.Pictureupload(map);//pcs为service中方法。
}

Controller(头像显示):

     @RequestMapping(params = "method=Pictureshows")
public ResponseEntity<byte[]> Pictureshow(HttpServletRequest request)throws Exception{
int YGID001 = (int)request.getSession().getAttribute("ygid001");
List<PictureShow> list=pcs.Pictureshows(YGID001);//根据员工号查找头像
byte[] bytes=list.get(0).getPortrait();
HttpHeaders headers = new HttpHeaders();
try {
headers.setContentType(MediaType.IMAGE_PNG);
headers.setContentDispositionFormData("attachment", new String("头像".getBytes("GBK"),"ISO8859-1"));
} catch (Exception e) {
e.printStackTrace();
}
return new ResponseEntity<byte[]>(bytes,headers,HttpStatus.OK);
}

sql:

<!--  头像插入   -->
insert into yg01 (SKYG018) values(#{in}) where YGID001=#{YGID001}
<!-- 头像显示 -->
select portrait from yg where YGID001=#{YGID001}

注:数据库字段类型设置为以下中的一种:TinyBlob 最大255 、 Blob 最大65K、 MediumBlob 最大16M、LongBlob  最大4G。

Java将头像图片保存到MySQL数据库的更多相关文章

  1. php将图片以二进制保存到mysql数据库并显示

    一.存储图片的数据表结构: -- -- 表的结构 `image` -- CREATE TABLE IF NOT EXISTS `image` ( `id` int(3) NOT NULL AUTO_I ...

  2. Python实现将图片以二进制格式保存到MySQL数据库中,以及取出:

    创建数据库表格式: CREATE TABLE photo ( photo_no int(6) unsigned NOT NULL auto_increment, image MEDIUMBLOB, P ...

  3. Python scrapy爬虫数据保存到MySQL数据库

    除将爬取到的信息写入文件中之外,程序也可通过修改 Pipeline 文件将数据保存到数据库中.为了使用数据库来保存爬取到的信息,在 MySQL 的 python 数据库中执行如下 SQL 语句来创建 ...

  4. node 爬虫 --- 将爬取到的数据,保存到 mysql 数据库中

    步骤一:安装必要模块 (1)cheerio模块 ,一个类似jQuery的选择器模块,分析HTML利器. (2)request模块,让http请求变的更加简单 (3)mysql模块,node连接mysq ...

  5. 使用官方组件下载图片,保存到MySQL数据库,保存到MongoDB数据库

    需要学习的地方,使用官方组件下载图片的用法,保存item到MySQL数据库 需要提前创建好MySQL数据库,根据item.py文件中的字段信息创建相应的数据表 1.items.py文件 from sc ...

  6. 利用session_set_save_handler()函数将session保存到MySQL数据库中

    PHP保存session默认的是采用的文件的方式来保存的,这仅仅在文件的空间开销很小的windows上是可以采用的,但是如果我们采用uinx或者是liux上的文件系统的时候,这样的文件系统的文件空间开 ...

  7. 关于阿里云centos 2.6下手机表情输入后无法保存到mysql数据库的问题调研及mysql版本从5.1升级到5.7的全过程纪要

    近日在开发手机app的评论功能时,输入表情文字,保存后提示数据库保存失败.错误日志片段如下 caused by: java.sql.SQLException: Incorrect string val ...

  8. 信息技术手册可视化进度报告 基于BeautifulSoup框架的python3爬取数据并连接保存到MySQL数据库

    老师给我们提供了一个word文档,里面是一份信息行业热词解释手册,要求我们把里面的文字存进数据库里面,然后在前台展示出来. 首先面临的问题是怎么把数据导进MySQL数据库,大家都有自己的方法,我采用了 ...

  9. python之scrapy爬取数据保存到mysql数据库

    1.创建工程 scrapy startproject tencent 2.创建项目 scrapy genspider mahuateng 3.既然保存到数据库,自然要安装pymsql pip inst ...

随机推荐

  1. 常见BOM实用对象

    1.跨浏览器获取窗口左边上边位置 2.IE9+.Chrome.Safari.Opera.Firefox支持下列属性. innerheight 返回浏览器窗口本身的高度. innerwidth  返回浏 ...

  2. 鸟哥linux私房菜学习笔记,U盘安装centos5.3不能正常引导的问题

    前言: 一直都想学习linux,毕竟是做测试的标配.听过鸟哥的linux私房菜大名,作为新手我淘来了第三版,到手看到书的厚度,心都凉了半截,本着不能浪费的原则,还是学吧! 过程:        开始看 ...

  3. 安装ruby compass失败

    安装compass失败 ERROR: Could not ), here is why: Unable to download data from https://rubygems.org/ - SS ...

  4. EF 关联数据查询

    1 直接使用表对应的实例属性值 ,如本例中的RoleUserInfo的属性UserInfo IEnumerable <RoleUserInfo > roleUserInfos= db.Ro ...

  5. seajs加载angularjs

    angularjs是自动完成模块的控制的,而seajs加载模块是异步的,所以不做修改,直接seajs加载angularjs会出错.            在这里讲下自己的解决方法 一.需要把ng-ap ...

  6. 【Windows 10 应用开发】输入模拟

    ---恢复内容开始--- Input Injection 直译为:输入注入.通俗的译法为:模拟输入.此注入行为可以模拟以下几种输入行为: 1.键盘按键. 2.鼠标. 3.触控. 4.书写笔输入. 5. ...

  7. python——文件操作

    open函数,该函数用于文件处理 操作文件时,一般需要经历如下步骤: 打开文件 操作文件 一.打开文件 1 文件句柄 = open('文件路径', '模式') 打开文件时,需要指定文件路径和以何等方式 ...

  8. 权限管理系统 mysql 数据脚本

    # SQL-Front 5.1 (Build 4.16) /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE */; /*!40101 SET SQL_MODE='STRICT ...

  9. 1012 u Calculate e

    A simple mathematical formula for e iswhere n is allowed to go to infinity. This can actually yield ...

  10. UISement属性

    .segmentedControlStyle 设置segment的显示样式. typedef NS_ENUM(NSInteger, UISegmentedControlStyle) { UISegme ...