Service层代码
@Service
public class UserChangeService {
@Autowired
private FastDFSClient fastDFSClient;
@Autowired
private TransactionTemplate template;
@Autowired
private GkshopUsersDirectoriesDOMapper directoriesDOMapper;
/**
* 手动发送邮件到移动
*
* @param listId 主建ID
* @return
* @author huaiyan
*/
public int sendEmailToMobile(List<Integer> listId) {
try {
if (StringUtil.isEmpty(listId)) {
return -1;
}
int result = template.execute(new TransactionCallback<Integer>() {
@Override
public Integer doInTransaction(TransactionStatus status) {
//根据主键批量查询对象
List<GkshopUserExchangeDO> list = queryExchangDoByID(listId);
if (StringUtil.isEmpty(list)) {
return -2;
}
//将查询出的信息作为一个map,以附件的形式发送给对方
List<Map<String, Object>> listMap = new ArrayList<>();
int size = list.size();
for (int i = 0; i < size; i++) {
Map<String, Object> map = new HashMap<>(16);
GkshopUserExchangeDO userExchangeDO = list.get(i);
if (StringUtil.isEmpty(userExchangeDO)) {
//对象为空,则跳过此对象
continue;
}
//得到手机号
String phoneNumber = userExchangeDO.getPhoneNumber();
//得到最新上传身份证的ID
Integer directoriesIdNew = userExchangeDO.getDirectoriesIdNew();
if (StringUtil.isEmpty(directoriesIdNew)) {
//实名人ID为空,则跳过此对象
continue;
}
//根据主键查询对象
GkshopUsersDirectoriesDO gkshopUsersDirectoriesDO = directoriesDOMapper.selectByPrimaryKey(directoriesIdNew);
if (StringUtil.isEmpty(gkshopUsersDirectoriesDO)) {
//实名人为空,则跳过此对象
continue;
}
//实名人姓名
String realName = gkshopUsersDirectoriesDO.getRealName();
//身份号
String identificationCard = gkshopUsersDirectoriesDO.getIdentificationCard();
//得到正面证件路径
String identificationPositive = gkshopUsersDirectoriesDO.getIdentificationPositive();
//得到反面证件路径
String identificationReverse = gkshopUsersDirectoriesDO.getIdentificationReverse();
byte[] positive = null;
byte[] reverse = null;
try {
positive = fastDFSClient.download(identificationPositive);
reverse = fastDFSClient.download(identificationReverse);
} catch (Exception e) {
logger.error("UserChangeService.doInTransaction error:" + e);
logger.info("UserChangeService.doInTransaction info={}", "fastDFS下载身份证发送异常!");
return -3;
}
map.put("phoneNumber", phoneNumber);
map.put("realName", realName);
map.put("identificationCard", identificationCard);
map.put("positive", positive);
map.put("reverse", reverse);
listMap.add(map);
}
InputStream iputStream = null;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
String[] columnName = {"变更手机号", "实名人", "身份证", "正面照", "反面照"};
String[] key= {"phoneNumber", "realName", "identificationCard", "positive", "reverse"};
try {
ExcelUtil.createBookAndInsertImage(listMap, key, columnName).write(outputStream);
outputStream.flush();
byte[] outPut = outputStream.toByteArray();
iputStream = new ByteArrayInputStream(outPut, 0, outPut.length);
} catch (Exception e) {
logger.error("UserChangeService.doInTransaction error:" + e);
logger.info("UserChangeService.doInTransaction info={}", "数据转化成输入流异常!");
return -4;
}
//获得抄送人
String messageTypeCc = SettingHelper.getSettingVal(SystemSettingEnums.SEND_EMAIL_EXCEL_TO, String.class);
//获得收件人
String messageTypeTo = SettingHelper.getSettingVal(SystemSettingEnums.SEND_EMAIL_EXCEL_CC, String.class);
//附件名字及后缀
String fuJian = "【极客科技】变更实名人申请汇总.xls";
//邮件主题
String subject = "【极客科技】手机卡号变更实名人申请汇总";
//邮件正文内容
String emailText = "这是一份有关于【极客科技】名下的手机卡号申请更换实名人的汇总邮件,详情查看附件!";
boolean flag = WordToEmailUtil.sendEmailToSomeOne(iputStream, subject, emailText, messageTypeTo, messageTypeCc, fuJian);
if (flag) {
//说明邮件发送成功了!
//修改这些数据的字段为已发送邮件,进度为已受理
GkshopUserExchangeQuery exchangeQuery = new GkshopUserExchangeQuery();
GkshopUserExchangeQuery.Criteria criteria = exchangeQuery.createCriteria();
criteria.andIdIn(listId);
GkshopUserExchangeDO gkshopUserExchangeDO = new GkshopUserExchangeDO();
//从已审核(2)-->已受理(3)
gkshopUserExchangeDO.setStatus(UserExChangStatusEnums.ALREADYACCEPT.getValue());
//最新发送邮件时间
gkshopUserExchangeDO.setUpdateTime(new Date());
gkshopUserExchangeDO.setSendStatus(1);
gkshopUserExchangeDO.setSendTime(new Date());
exchangeDOMapper.updateByExampleSelective(gkshopUserExchangeDO, exchangeQuery);
return 1;
} else {
return -5;
}
}
});
return result;
} catch (Exception e) {
logger.error("UserChangeService.sendEmailToMobile error:" + e);
return -6;
}
}
}
fastDFS工具类
package com.geenk.market.common.util.fastdfs.client;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.FileInfo;
import org.csource.fastdfs.ProtoCommon;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.TrackerServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* FastDFS Java API. 文件上传下载主类.
*
* @author 120841
* @date 2018年12月18日16:29:29
*/
@Component
public class FastDFSClient {
/***
*
*/
public static final String KEY = "GeenkShop123!@#";
/**
* 路径分隔符
*/
public static final String SEPARATOR = "/";
/**
* Point
*/
public static final String POINT = ".";
/**
* ContentType
*/
public static final Map<String, String> EXT_MAPS = new HashMap<>();
/**
* org.slf4j.Logger
*/
private static Logger logger = LoggerFactory.getLogger(FastDFSClient.class);
/**
* 文件名称Key
*/
private static final String FILENAME = "filename";
/**
* 文件最大的大小
*/
private int maxFileSize = 100 * 1000 * 1000;
private FastDFSClient fastDFSClient = null;
@PostConstruct
private void initFastDFSClient(){
if (fastDFSClient == null) {
fastDFSClient = new FastDFSClient();
}
}
public FastDFSClient() {
initExt();
}
private void initExt() {
// image
EXT_MAPS.put("png", "image/png");
EXT_MAPS.put("gif", "image/gif");
EXT_MAPS.put("bmp", "image/bmp");
EXT_MAPS.put("ico", "image/x-ico");
EXT_MAPS.put("jpeg", "image/jpeg");
EXT_MAPS.put("jpg", "image/jpeg");
// 压缩文件
EXT_MAPS.put("zip", "application/zip");
EXT_MAPS.put("rar", "application/x-rar");
// doc
EXT_MAPS.put("pdf", "application/pdf");
EXT_MAPS.put("ppt", "application/vnd.ms-powerpoint");
EXT_MAPS.put("xls", "application/vnd.ms-excel");
EXT_MAPS.put("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
EXT_MAPS.put("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation");
EXT_MAPS.put("doc", "application/msword");
EXT_MAPS.put("doc", "application/wps-office.doc");
EXT_MAPS.put("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
EXT_MAPS.put("txt", "text/plain");
// 音频
EXT_MAPS.put("mp4", "video/mp4");
EXT_MAPS.put("flv", "video/x-flv");
}
/**
* MultipartFile 上传文件
*
* @param file MultipartFile
* @return 返回上传成功后的文件路径
*/
public String uploadFileWithMultipart(MultipartFile file) throws FastDFSException {
return upload(file, null);
}
/**
* MultipartFile 上传文件
*
* @param file MultipartFile
* @param descriptions 文件描述
* @return 返回上传成功后的文件路径
*/
public String uploadFileWithMultipart(MultipartFile file, Map<String, String> descriptions) throws FastDFSException {
return upload(file, descriptions);
}
/**
* 根据指定的路径上传文件
*
* @param filepath 文件路径
* @return 返回上传成功后的文件路径
*/
public String uploadFileWithFilepath(String filepath) throws FastDFSException {
return upload(filepath, null);
}
/**
* 根据指定的路径上传文件
*
* @param filepath 文件路径
* @param descriptions 文件描述
* @return 返回上传成功后的文件路径
*/
public String uploadFileWithFilepath(String filepath, Map<String, String> descriptions) throws FastDFSException {
return upload(filepath, descriptions);
}
/**
* 上传base64文件
*
* @param base64 文件base64
* @return 返回上传成功后的文件路径
*/
public String uploadFileWithBase64(String base64) throws FastDFSException {
return upload(base64, null, null);
}
/**
* 上传base64文件
*
* @param base64 文件base64
* @param filename 文件名
* @return 返回上传成功后的文件路径
*/
public String uploadFileWithBase64(String base64, String filename) throws FastDFSException {
return upload(base64, filename, null);
}
/**
* 上传base64文件
*
* @param base64 文件base64
* @param filename 文件名
* @param descriptions 文件描述信息
* @return 返回上传成功后的文件路径
*/
public String uploadFileWithBase64(String base64, String filename, Map<String, String> descriptions) throws FastDFSException {
return upload(base64, filename, descriptions);
}
/**
* 使用 MultipartFile 上传
*
* @param file MultipartFile
* @param descriptions 文件描述信息
* @return 文件路径
* @throws FastDFSException file为空则抛出异常
*/
public String upload(MultipartFile file, Map<String, String> descriptions) throws FastDFSException {
if (file == null || file.isEmpty()) {
throw new FastDFSException(ErrorCode.FILE_ISNULL.CODE, ErrorCode.FILE_ISNULL.MESSAGE);
}
String path = null;
try {
path = upload(file.getInputStream(), file.getOriginalFilename(), descriptions);
} catch (IOException e) {
logger.error("FastDFSClient.upload error:",e);
throw new FastDFSException(ErrorCode.FILE_ISNULL.CODE, ErrorCode.FILE_ISNULL.MESSAGE);
}
return path;
}
/**
* 根据指定的路径上传
*
* @param filepath 文件路径
* @param descriptions 文件描述
* @return 文件路径
* @throws FastDFSException 文件路径为空则抛出异常
*/
public String upload(String filepath, Map<String, String> descriptions) throws FastDFSException {
if (StringUtils.isBlank(filepath)) {
throw new FastDFSException(ErrorCode.FILE_PATH_ISNULL.CODE, ErrorCode.FILE_PATH_ISNULL.MESSAGE);
}
File file = new File(filepath);
String path = null;
try {
InputStream is = new FileInputStream(file);
// 获取文件名
filepath = toLocal(filepath);
String filename = filepath.substring(filepath.lastIndexOf("/") + 1);
path = upload(is, filename, descriptions);
} catch (FileNotFoundException e) {
logger.error("FastDFSClient.upload error:",e);
throw new FastDFSException(ErrorCode.FILE_NOT_EXIST.CODE, ErrorCode.FILE_NOT_EXIST.MESSAGE);
}
return path;
}
/**
* 上传base64文件
*
* @param base64
* @param filename 文件名
* @param descriptions 文件描述信息
* @return 文件路径
* @throws FastDFSException base64为空则抛出异常
*/
public String upload(String base64, String filename, Map<String, String> descriptions) throws FastDFSException {
if (StringUtils.isBlank(base64)) {
throw new FastDFSException(ErrorCode.FI