上传,下载身份证案例

package site.action.ecom.mejtao.personalinfo;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import rop.thirdparty.com.alibaba.fastjson.JSON;
import site.base.action.FrontendBaseAction;
import site.base.businessbean.FileBean;
import site.base.pojo.FrontUser;
import site.base.service.BaseService;
import site.common.PropertyUtil;
import site.common.constant.Constants;
import site.service.ecom.customer.CustomerService;
import site.vo.ecom.customer.CustomerBizBean;

@ParentPackage("base_for_front_json")
@Namespace("/personalinfo")
public class PersonalInfoAction extends FrontendBaseAction{


private static final long serialVersionUID = 1L;

@Autowired
private CustomerService customerService;

private String realName;
private String identify;

private Long supplierHeadId;
private Long customerId;
//文件
private File files;
private File files2;


//文件原名
private String filesFileName;
private String files2FileName;
private ByteArrayInputStream inputStream;
private String flag;
private String showflag;
/**
* 保存,更新身份信息
* @return
*/
@Action(value="updateinfo")
public String updateInfo(){
boolean single=false;
FrontUser user= super.getCurrentFrontUser();
CustomerBizBean customBean = customerService.getCustomerById(user.getId());//
customBean.setCustomerHeadName(realName);
customBean.setCustomerHeadIdNo(identify);
try{
customerService.execute(customBean,BaseService.COMMAND_UPDATE);
single=true;
}catch(Exception e){
e.printStackTrace();
}
getResultMap().put("result", single);
return RES_JSON;
}

/**
* 上传身份证
*/
@Action(value="personinfo")
public void personInfo(){
FrontUser user= super.getCurrentFrontUser();
Map<String,Object> map = new HashMap<String, Object>();
CustomerBizBean customBean = customerService.getCustomerById(user.getId());//

boolean fileFlag = false;
String filePath = null;
String fileName = null;

//取得上传文件类型
String uploadFileType=PropertyUtil.getPropertyValueByKey(Constants.COMMMON_PIC_TYPE,request);
//取得上传文件大小
String uploadFileSize=PropertyUtil.getPropertyValueByKey(Constants.SUPP_FILESIZE,request);
//取得上传文件路径
String myUploadDir;
myUploadDir=getUploadRootDirForService();
setFileAllowedTypes(uploadFileType);
//按日期存放
File dirFile = new File(myUploadDir+"/identity");
if(!(dirFile.exists())&&!(dirFile.isDirectory())){
boolean creadok = dirFile.mkdirs();
if (creadok) {
myUploadDir=myUploadDir+"/identity";
}
}else{
myUploadDir=myUploadDir+"/identity" ;
}
setUploadDir1(myUploadDir);
setFileMaxSize(Long.valueOf(uploadFileSize));
String newfilename="";
if("front".equals(flag)){
newfilename=customBean.getCustomerHeadLoginNo()+"_front"+
filesFileName.substring(filesFileName.indexOf("."),filesFileName.length());
}else if("back".equals(flag)){
newfilename=customBean.getCustomerHeadLoginNo()+"_back"+
files2FileName.substring(files2FileName.indexOf("."),files2FileName.length());
}

//上传
try {
if(files != null || files2 !=null){
FileBean fileBean1=new FileBean();
if("front".equals(flag)){
fileBean1=super.uploadFile(files,filesFileName, newfilename);
}else if("back".equals(flag)){
fileBean1=super.uploadFile(files2,files2FileName, newfilename);
}

//supplierHeadBean.setSupplierFeeFile(fileBean1.getNewName()) ;
filePath = myUploadDir;
fileName = fileBean1.getNewName();
fileFlag = true;
if("front".equals(flag)){
customBean.setCustomerIdNoFront(myUploadDir+"/"+newfilename);
}else if("back".equals(flag)){
customBean.setCustomerIdNoBack(myUploadDir+"/"+newfilename);
}

customerService.execute(customBean,BaseService.COMMAND_UPDATE);
}
} catch (Exception e) {
log.error("图片上传失败!");
this.addFieldError("upload", "上传图片失败!");
}


map.put("filePath", filePath);
map.put("fileName", fileName);
map.put("fileFlag", fileFlag);

try {
response.getWriter().write(JSON.toJSONString(map));
response.getWriter().flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}
/**
* 下载身份证
* @return
* @throws Exception
*/
@Action(value = "downpic", results= {@Result(name=SUCCESS,type="stream",params={"contentType","image/jpeg","inputName","inputStream"})})
public String downPic() throws Exception {
setInputStream(createInputStream());
return SUCCESS;
}
// 将图片的以字节形式写到InputStream里
public ByteArrayInputStream createInputStream() throws Exception {
FrontUser user= super.getCurrentFrontUser();
CustomerBizBean customBean = customerService.getCustomerById(user.getId());
String imgPath="";
if("front".equals(showflag)){
imgPath = customBean.getCustomerIdNoFront();
}else if("back".equals(showflag)){
imgPath = customBean.getCustomerIdNoBack();
}

BufferedImage image = ImageIO.read(new FileInputStream(imgPath));

ByteArrayOutputStream output = new ByteArrayOutputStream();
ImageOutputStream imageOut = ImageIO.createImageOutputStream(output);
ImageIO.write(image, "JPEG", imageOut);
imageOut.close();
ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
output.close();
return input;
}

public Long getSupplierHeadId() {
return supplierHeadId;
}
public void setSupplierHeadId(Long supplierHeadId) {
this.supplierHeadId = supplierHeadId;
}
public Long getCustomerId() {
return customerId;
}
public void setCustomerId(Long customerId) {
this.customerId = customerId;
}

public void setFiles(File files) {
this.files = files;
}

public void setFilesFileName(String filesFileName) {
this.filesFileName = filesFileName;
}
public ByteArrayInputStream getInputStream() {
return inputStream;
}
public void setInputStream(ByteArrayInputStream inputStream) {
this.inputStream = inputStream;
}

public String getRealName() {
return realName;
}

public void setRealName(String realName) {
this.realName = realName;
}

public String getIdentify() {
return identify;
}

public void setIdentify(String identify) {
this.identify = identify;
}

public String getFlag() {
return flag;
}

public void setFlag(String flag) {
this.flag = flag;
}

public File getFiles2() {
return files2;
}

public void setFiles2(File files2) {
this.files2 = files2;
}

public String getFiles2FileName() {
return files2FileName;
}

public void setFiles2FileName(String files2FileName) {
this.files2FileName = files2FileName;
}

public String getShowflag() {
return showflag;
}

public void setShowflag(String showflag) {
this.showflag = showflag;
}




}

 

 

 

 

define(function(require, exports, module) {
var $base = require('pageinit');// 引入带滚动翻页的基础js
var $ = $base.query;// 获取模块的jquery全局变量
require('jquery/jquery.ajaxfileupload')($);

$(document).ready(function(){
download()
returnurl=getQueryString("returnurl");
});
});

//返回
var returnurl;
function goback(){

if(returnurl==""){
returnurl="/html/persona/my.html";
}
returnurl="http://"+window.location.hostname+":"+window.location.port+returnurl;
//window.location.hostname+":"+window.location.port "http://localhost:8080"
window.location.href=returnurl;
}

var falg;
var file;
function uploadfront(){
if($('#files').val()==""){
alert("请选择一张图片");
return;
}
flag="front";
file="files";
upload();
}
function uploadback(){
if($('#files2').val()==""){
alert("请选择一张图片");
return;
}
flag="back";
file="files2";
upload();
}


//上传图片
function upload(){
$.ajaxFileUpload({ url : '/personalinfo/personinfo.do?flag='+flag,
secureuri : false,
fileElementId : file,
async: false,
// dataType : 'json',
success : function(data,status) {
alert("上传成功");
download();
},
error : function(data, status, e) {
alert("上传失败");
}
});

}
//下载图片
function download(){
var imgfront=document.getElementById("idimage");
imgfront.src="/personalinfo/downpic.do?showflag=front";
var imgback=document.getElementById("idimage2");
imgback.src="/personalinfo/downpic.do?showflag=back"
}
//保存个人信息
function updateinfo(){
var realname=$("#realname").val();
var identiy=$("#identifi").val();
if(realname.trim()=="" || realname==null){
alert("请输入真实姓名!");
return;
}
if(identiy.trim()=="" || identiy==null){
alert("请输入正确身份证!");
return;
}
$.getJSON("/personalinfo/updateinfo.do",
{"realName":realname,"identify":identiy},
function(element){
var f=element.result;
if(f){
alert("保存成功!");
}else{
alert("信息保存失败");
}
});

}

 

 

 

 

 

<table>
<tr>
<td><a class="f_l MR10" href="javascript:addImg();"><img id="idimage" width="100" height="100" src=""></a></td>
<td><a class="f_l MR10" href="javascript:addImg();"><img id="idimage2" width="100" height="100" src=""></a></td>
</tr>
<tr>
<td><input id="files" name="files" type="file" onchange="uploadfront()"/><br></td>
<td><input id="files2" name="files2" type="file" onchange="uploadback()"/><br></td>
</tr>
</table>

posted @ 2015-06-03 14:23  萧雪痕  阅读(680)  评论(1编辑  收藏  举报