第一个问题:如何通过Extjs4实现照片上传的布局展示以及本地照片选择后的在一个区域内进行图片预览

实现照片上传的布局展示:

items : [ {
xtype : 'box',
itemId : 'imageShow',
id:'imageShow',
border : 1,
style : {
borderColor : '#99bbe8',
borderStyle : 'solid',
},
margin : '0 0 5 55',
autoEl : {
width : 105,
height : 128,
tag : 'img',
//style : 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale);',
complete : 'off',
src : 'images/blank.jpg',
}
}, {
xtype : 'filefield',
name : 'imageFile',
itemId:'imageFile',
id:'person_imageFile',
labelWidth : 50,
width : 200,
fieldLabel : '照片',
allowBlank : false,
buttonText: '',
buttonConfig: {
iconCls: 'upload-icon'
}
} ]

显示的样式为:

 

第二个问题:如何在chrome中选择本地的照片后进行图片的预览:(对file控件进行监控)

'#personWindow #imageFile' : {
change : this.handlerImageFile
},
handlerImageFile:function(view,value){
var file=Ext.query('#person_imageFile input[type=file]')[0];
var img=view.previousSibling().getEl().dom;
var reader = new FileReader();
reader.onload = function(evt){img.src = evt.target.result;}
reader.readAsDataURL(file.files[0]);
},

 

第三个问题:如何跟SpringMVC结合保存照片呢?

1.含有照片的form,必须要通过form.submit来进行提交:

savePersonAction:function(button,urlString){
var form = button.up('window').down('form').getForm();
form.submit({
clientValidation : true,
url : urlString,
params : {
// newStatus: 'delivered'
},
scope : this,// 使回调函数中的this变成当前的类
success : function(form, action) {
if (action.result.success == 'true') {
Ext.Msg.alert('系统提示', action.result.msg,function(){
button.up('window').hide();
this.refreshGrid();
},this);
} else {
Ext.Msg.alert('系统提示', action.result.errorMsg);
}
},
});
}

 

2.在spring的配置文件中增加配置:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="100000"/>
<property name="maxInMemorySize" value="4096" />
</bean>

 

3.在bean中增加配置

private Date csrq;
private String jtzz;
private String bz;
private Integer photoId;
private CommonsMultipartFile imageFile;//上传的文件

 

4.使用bean来接受参数:

@ResponseBody
@RequestMapping("/userManage/person/insertPerson")
public String insertPerson(SysPersonModel sysPersonModel) {
Map resultMap = new HashMap();
try {
sysPersonService.insertPerson(sysPersonModel);
resultMap.put("success", "true");
resultMap.put("msg", "保存成功");
} catch (ApplicationException e) {
resultMap.put("success", "ApplicationException");
resultMap.put("errorMsg", e.getMessage());
} catch (Exception e) {
e.printStackTrace();
resultMap.put("success", "Exception");
resultMap.put("errorMsg", e.getMessage());
} return JSON.toJSONString(resultMap);
}

 

第四个问题:如何在Mybatis中进行文件的保存和查看呢?

1.增加Photo相关的Bean以及Mapper类,以及sql文件:

package cn.telchina.standard.entity;

public class PhotoModel {
private Integer photoId;
private String photoName;
private Object photo; public Integer getPhotoId() {
return photoId;
} public void setPhotoId(Integer photoId) {
this.photoId = photoId;
} public String getPhotoName() {
return photoName;
} public void setPhotoName(String photoName) {
this.photoName = photoName;
} public Object getPhoto() {
return photo;
} public void setPhoto(Object photo) {
this.photo = photo;
} }

 

package cn.telchina.standard.mapper;

import java.util.List;

import cn.telchina.standard.entity.PhotoModel;

public interface PhotoMapper {

    public int getPhotoId();

    public void createPhoto(PhotoModel photoModel);

    public int deletePhoto(int photoId);

    public int updatePhoto(PhotoModel photo);

    public  List<PhotoModel> getPhotoForUpdate(int photoId);

    public  List<PhotoModel> getPhoto(int photoId);

}

 

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.telchina.standard.mapper.PhotoMapper">
<!-- <![CDATA[sql 尽量每个sql必须写,防止有些特殊字符对sql语句造成的破坏 -->
<select id="getPhotoId" resultType="int">
<![CDATA[
select base.seq_photo.nextval id from dual
]]>
</select> <insert id="createPhoto" parameterType="PhotoModel">
<![CDATA[
INSERT INTO base.PHOTO(photoid,photoname,photo)
VALUES(#{photoId},#{photoName, jdbcType=VARCHAR}, empty_blob())
]]>
</insert> <select id="getPhotoForUpdate" resultType="PhotoModel">
SELECT photoid,
photo,
photoname
FROM base.PHOTO
WHERE photoid = #{photoId} for update
</select> <delete id="deletePhoto" >
DELETE FROM base.PHOTO
WHERE PHOTO_ID = #{photoId}
</delete> <update id="updatePhoto" parameterType="PhotoModel" >
UPDATE base.PHOTO
SET photoname = #{photoName, jdbcType=VARCHAR}
WHERE photoid = #{photoId}
</update> <select id="getPhoto" resultType="PhotoModel">
SELECT photoid,
photo,
photoname
FROM base.PHOTO
WHERE photoid = #{photoId}
</select>
</mapper>

2.在service中增加新增照片的代码:

@Transactional(rollbackFor = Exception.class)
public void insertPerson(SysPersonModel sysPersonModel)
throws ApplicationException {
this.checkPersonByRybh(sysPersonModel.getRybh());
this.checkPersonBySfzhm(sysPersonModel.getSfzhm()); int photoId = photoMapper.getPhotoId();
String fileName = sysPersonModel.getImageFile().getOriginalFilename(); PhotoModel photoModel = new PhotoModel();
photoModel.setPhotoId(photoId);
photoModel.setPhotoName(fileName); photoMapper.createPhoto(photoModel); updatePhoto(sysPersonModel, photoId); sysPersonModel.setPhotoId(photoId); sysPersonMapper.insertPerson(sysPersonModel);
} private void updatePhoto(SysPersonModel sysPersonModel, Integer photoId) {
List<PhotoModel> list = photoMapper.getPhotoForUpdate(photoId);
PhotoModel newPhotoModel = list.get(0); BLOB photo = (BLOB) newPhotoModel.getPhoto(); BufferedInputStream in = null;
BufferedOutputStream out = null;
try {
out = new BufferedOutputStream(
photo.getBinaryOutputStream());// 暂时使用这个废弃的方法
// ops = content.setBinaryStream(0);//ojdbc14支持,ojdbc6,5都不支持
in = new BufferedInputStream(
(FileInputStream) sysPersonModel.getImageFile()
.getInputStream());
byte[] data = FileCopyUtils.copyToByteArray(in);
out.write(data);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

3文件的展示和下载:

前台代码:

//进入新增页面时初始化默认头像
initImageSrc:function(view){
if(view.down('#imageShow').getEl()){
var img=view.down('#imageShow').getEl().dom;
img.src='images/blank.jpg';
}
},
//查看
showImg:function(view,photoId){
if(photoId!=""){
var img=view.down('#imageShow').getEl().dom;
img.src='userManage/person/showPhoto.json?photoId='+photoId;
}else{
this.initImageSrc(view);
}
},
//下载
downFileButtonHandler:function(button){
var sm = button.up('#datagrid').getSelectionModel();
if (sm.getCount() == 0) {
Ext.Msg.alert('系统提示', "修改人员信息前先选中一条记录!");
} else {
var record = sm.getSelection()[0];
var photoId=record.data.photoId;
if(photoId!=""){
window.open('userManage/person/downFile.json?photoId='+photoId,"_self");
}else{
Ext.Msg.alert('系统提示', "该人员没有头像!");
} }
},
@ResponseBody
@RequestMapping(value = "/userManage/person/showPhoto")
public void showPhoto(Integer photoId, HttpServletResponse response) {
OutputStream outputStream = null;
InputStream in = null;
try {
PhotoModel photoModel = sysPersonService.getPhotoById(photoId); BLOB photo = (BLOB) photoModel.getPhoto(); response.setContentType("image/jpeg");
response.setCharacterEncoding("UTF-8");
outputStream = new BufferedOutputStream(response.getOutputStream());
in = new BufferedInputStream(photo.getBinaryStream()); int len = 0;
byte[] buf = new byte[1024];
while ((len = in.read(buf, 0, 1024)) != -1) {
outputStream.write(buf, 0, len);
}
} catch (ApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
in.close();
outputStream.flush();
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
} @ResponseBody
@RequestMapping(value = "/userManage/person/downFile")
public void downFile(Integer photoId, HttpServletResponse response) {
OutputStream outputStream = null;
InputStream in = null; try {
PhotoModel photoModel = sysPersonService.getPhotoById(photoId); BLOB photo = (BLOB) photoModel.getPhoto(); // byte[] data=photo.getBytes(); String fileName = photoModel.getPhotoName() == null ? "照片.jpg"
: photoModel.getPhotoName();
fileName = URLEncoder.encode(fileName, "UTF-8");
response.reset();
response.setHeader("Content-Disposition", "attachment; filename=\""
+ fileName + "\"");
response.addHeader("Content-Length", "" + photo.length());
response.setContentType("application/octet-stream;charset=UTF-8"); in = new BufferedInputStream(photo.getBinaryStream());
outputStream = new BufferedOutputStream(response.getOutputStream()); int len = 0;
byte[] buf = new byte[1024];
while ((len = in.read(buf, 0, 1024)) != -1) {
outputStream.write(buf, 0, len);
}
} catch (ApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
in.close();
outputStream.flush();
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
}

EXTjs+SpringMVC+Mybatis实现照片的上传,下载,查看关键技术整理的更多相关文章

  1. SpringMVC——返回JSON数据&&文件上传下载

    --------------------------------------------返回JSON数据------------------------------------------------ ...

  2. springmvc 多文件/文件夹上传 下载

    注入依赖 <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding&g ...

  3. CentOS上传下载查看命令

    之前往CentOS中上传都用ftp软件,这里介绍一种另外的上传下载方式,两个命令轻松搞定.这两个命令目前只针对Xshell和SecureCRT等远程终端软件才支持,并且还会有时间的限制.大概30秒不上 ...

  4. extjs插件开发上传下载文件简单案例

    前台,extjs,框架,mybatis,spring,springMVC,简单的文件上传下载案例. 必要的jar包,commons-fileupload-1.3.1.jar,commons-io-2. ...

  5. SpringMVC整合fastdfs-client-java实现web文件上传下载

    原文:http://blog.csdn.net/wlwlwlwl015/article/details/52682153 本篇blog主要记录一下SpringMVC整合FastDFS的Java客户端实 ...

  6. springmvc文件上传下载简单实现案例(ssm框架使用)

    springmvc文件上传下载实现起来非常简单,此springmvc上传下载案例适合已经搭建好的ssm框架(spring+springmvc+mybatis)使用,ssm框架项目的搭建我相信你们已经搭 ...

  7. SpringMVC实现上传下载功能

    配置资源(jar包) 将前端页面整理好: 写核心的几个配置文件(applicationContext+wed.xml+jdbc.properties+log4j+springMVC.xml) 都是在s ...

  8. SpringMVC文件上传下载(单文件、多文件)

    前言 大家好,我是bigsai,今天我们学习Springmvc的文件上传下载. 文件上传和下载是互联网web应用非常重要的组成部分,它是信息交互传输的重要渠道之一.你可能经常在网页上传下载文件,你可能 ...

  9. springmvc 上传下载

    springmvc文件上传下载在网上搜索的代码 参考整理了一份需要使用的jar.commons-fileupload.jar与commons-io-1.4.jar 二个文件 1.表单属性为: enct ...

随机推荐

  1. 洛谷——P1579 哥德巴赫猜想(升级版)

    P1579 哥德巴赫猜想(升级版) 题目背景 1742年6月7日哥德巴赫写信给当时的大数学家欧拉,正式提出了以下的猜想:任何一个大于9的奇数都可以表示成3个质数之和.质数是指除了1和本身之外没有其他约 ...

  2. HashMap和Hashtable 线程安全性

    HashMap和Hashtable的比较是Java面试中的常见问题,用来考验程序员是否能够正确使用集合类以及是否可以随机应变使用多种思路解决问题.HashMap的工作原理.ArrayList与Vect ...

  3. 使用CXF开发RESTFul服务

    相信大家在阅读CXF官方文档(http://cxf.apache.org/docs/index.html)时,总是一知半解.这里向大家推荐一本PacktPub.Apache.CXF.Web.Servi ...

  4. 八. 输入输出(IO)操作7.文件的随机读写

    Java.io 包提供了 RandomAccessFile 类用于随机文件的创建和访问.使用这个类,可以跳转到文件的任意位置读写数据.程序可以在随机文件中插入数据,而不会破坏该文件的其他数据.此外,程 ...

  5. 转换vmware的vmdk格式到qcow2或者raw格式

    qemu-img convert  xxxx-disk1.vmdk -f  vmdk  -O qcow2 xxxx-disk1.qcow2 qemu-img convert xxxx-disk1.vm ...

  6. js常用函数和常用技巧

    学习和工作的过程中总结的干货,包括常用函数.常用js技巧.常用正则表达式.git笔记等.为刚接触前端的童鞋们提供一个简单的查询的途径,也以此来缅怀我的前端学习之路. PS:此文档,我会持续更新. Aj ...

  7. iOS app测试的福音--TestFlight使用说明

    Here's What's New: Invite up to 1,000 external testers using just their email address Easy to use Te ...

  8. hdu1021(C++)

    打表找规律,发现是n%4==2就是yes,否则是no #include<iostream>using namespace std;int main(){ int n; while (cin ...

  9. C++STL中的向量vector

    #include<iostream>#include<vector>#include<algorithm>using namespace std;typedef v ...

  10. 路由器漏洞复现分析第二弹:CNVD-2018-01084

    1月17日,CNVD公开了D-LinkDIR 615/645/815 service.cgi远程命令执行漏洞(CNVD-2018-01084),freebuf上有前辈写了一篇漏洞复现和poc的文章(h ...