Commit 97845ba8 by admin

添加文件上传更新

parent 9cab1b25
...@@ -9,6 +9,7 @@ import java.util.List; ...@@ -9,6 +9,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.TimeZone; import java.util.TimeZone;
import org.bson.Document;
import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
...@@ -22,6 +23,7 @@ import org.slf4j.LoggerFactory; ...@@ -22,6 +23,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.zhiwei.util.Config; import com.zhiwei.util.Config;
import com.zhiwei.util.MongoConnection;
import com.zhiwei.util.ReadExcel; import com.zhiwei.util.ReadExcel;
import com.zw.entity.NewsResult; import com.zw.entity.NewsResult;
...@@ -36,6 +38,7 @@ public class ESUtil { ...@@ -36,6 +38,7 @@ public class ESUtil {
private static final Logger log = LoggerFactory.getLogger(ESUtil.class); private static final Logger log = LoggerFactory.getLogger(ESUtil.class);
private static String indexName; private static String indexName;
private static String type; private static String type;
private static MongoConnection con=new MongoConnection();
static { static {
indexName = Config.getVal("indexName"); indexName = Config.getVal("indexName");
...@@ -130,8 +133,15 @@ public class ESUtil { ...@@ -130,8 +133,15 @@ public class ESUtil {
try { try {
log.info("推送检索开始"); log.info("推送检索开始");
Client esClient = ESClient.getInstance(); Client esClient = ESClient.getInstance();
List<String> keys = new ReadExcel() List<String> keys = new ArrayList<String>();
.getExcel(ESUtil.class.getClassLoader().getResource("pushFiles/pushkey.xls").getPath()); String path = ESUtil.class.getClassLoader().getResource("pushFiles/pushkey.xls").getPath();
Document doc = con.find("financePush", "excelPath", new Document().append("name", "key"));
if (doc != null) {
if (doc.getString("url") != null) {
path = doc.getString("url");
}
}
keys = new ReadExcel().getExcel(path);
for (String key : keys) { for (String key : keys) {
// 搜索数据 // 搜索数据
SearchRequestBuilder srb = esClient.prepareSearch(indexName).setTypes(type); SearchRequestBuilder srb = esClient.prepareSearch(indexName).setTypes(type);
......
package com.zhiwei.finance; package com.zhiwei.finance;
import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.bson.Document;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.zhiwei.orm.BeanFactory; import com.zhiwei.orm.BeanFactory;
...@@ -17,26 +19,28 @@ import com.zhiwei.util.StringUtil; ...@@ -17,26 +19,28 @@ import com.zhiwei.util.StringUtil;
import com.zw.entity.NewsResult; import com.zw.entity.NewsResult;
import sendmail.SendMailUtil; import sendmail.SendMailUtil;
@Component @Component
public class PushFinanceByTime extends Thread { public class PushFinanceByTime extends Thread {
private static final Log log = LogFactory.getLog(PushFinanceByTime.class); private static final Log log = LogFactory.getLog(PushFinanceByTime.class);
private static final MongoConnection con=new MongoConnection(); private static final MongoConnection con = new MongoConnection();
public PushFinanceByTime() { public PushFinanceByTime() {
this.start(); this.start();
} }
public void run() { public void run() {
Services service=BeanFactory.getBean(Services.class);
if(!StringUtil.isEmpty(service.getPath("longSource"))) {
}
List<String> sources = new ReadExcel()
.getExcel(ESUtil.class.getClassLoader().getResource("pushFiles/pushSource.xls").getPath());
while (true) { while (true) {
List<String> sources = new ArrayList<String>();
String path = PushFinanceByTime.class.getClassLoader().getResource("pushFiles/pushSource.xls").getPath();
Document doc = con.find("financePush", "excelPath", new Document().append("name", "longSource"));
if (doc != null) {
if (doc.getString("url") != null) {
path = doc.getString("url");
}
}
log.info("推送检测"); log.info("推送检测");
sources = new ReadExcel().getExcel(path);
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
try { try {
if (cal.get(Calendar.HOUR_OF_DAY) == 8 && cal.get(Calendar.MINUTE) >= 30) { if (cal.get(Calendar.HOUR_OF_DAY) == 8 && cal.get(Calendar.MINUTE) >= 30) {
...@@ -66,6 +70,7 @@ public class PushFinanceByTime extends Thread { ...@@ -66,6 +70,7 @@ public class PushFinanceByTime extends Thread {
+ (int) (Math.random() * 5000) + "sends.xls"; + (int) (Math.random() * 5000) + "sends.xls";
cal.add(Calendar.HOUR_OF_DAY, 5); cal.add(Calendar.HOUR_OF_DAY, 5);
sendsEmail(cal, list, url); sendsEmail(cal, list, url);
sleep(1000 * 60 * 60); sleep(1000 * 60 * 60);
} }
} }
...@@ -73,15 +78,33 @@ public class PushFinanceByTime extends Thread { ...@@ -73,15 +78,33 @@ public class PushFinanceByTime extends Thread {
} catch (Exception e) { } catch (Exception e) {
log.error("推送错误信息", e); log.error("推送错误信息", e);
} }
try {
sleep(1000 * 10);
} catch (InterruptedException e) {
e.printStackTrace();
}
} }
} }
private void sendsEmail(Calendar cal, List<NewsResult> list, String url) throws Exception { private void sendsEmail(Calendar cal, List<NewsResult> list, String url) {
new ExcelUtil().pushDataWithFinance(list, cal.getTime(), url); try {
SendMailUtil.sendMessage("949131080@qq.com", url, "金融"); new ExcelUtil().pushDataWithFinance(list, cal.getTime(), url);
SendMailUtil.sendMessage("yuhualong@zhiweidata.com", url, "金融"); SendMailUtil.sendMessage("949131080@qq.com", url, "金融");
SendMailUtil.sendMessage("ligaodie@zhiweidata.com", url, "金融"); SendMailUtil.sendMessage("yuhualong@zhiweidata.com", url, "金融");
ExcelUtil.delete(url); SendMailUtil.sendMessage("ligaodie@zhiweidata.com", url, "金融");
ExcelUtil.delete(url);
} catch (Exception e) {
log.error("一号邮箱发送失败,尝试二号邮箱{}", e);
try {
new ExcelUtil().pushDataWithFinance(list, cal.getTime(), url);
SendMailUtil.sendMessage2("949131080@qq.com", url, "金融");
SendMailUtil.sendMessage2("yuhualong@zhiweidata.com", url, "金融");
SendMailUtil.sendMessage2("ligaodie@zhiweidata.com", url, "金融");
ExcelUtil.delete(url);
} catch (Exception e2) {
log.error("二号邮箱发送错误{}", e);
}
}
} }
} }
package com.zhiwei.finance; package com.zhiwei.finance;
import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.bson.Document;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.zhiwei.util.ExcelUtil; import com.zhiwei.util.ExcelUtil;
import com.zhiwei.util.MongoConnection;
import com.zhiwei.util.ReadExcel; import com.zhiwei.util.ReadExcel;
import com.zw.entity.NewsResult; import com.zw.entity.NewsResult;
...@@ -17,15 +20,25 @@ import sendmail.SendMailUtil; ...@@ -17,15 +20,25 @@ import sendmail.SendMailUtil;
@Component @Component
public class PushFinanceByTimeLess extends Thread { public class PushFinanceByTimeLess extends Thread {
private static final Log log = LogFactory.getLog(PushFinanceByTimeLess.class); private static final Log log = LogFactory.getLog(PushFinanceByTimeLess.class);
private static final MongoConnection con = new MongoConnection();
public PushFinanceByTimeLess() { public PushFinanceByTimeLess() {
this.start(); this.start();
} }
public void run() { public void run() {
List<String> sources = new ReadExcel()
.getExcel(ESUtil.class.getClassLoader().getResource("pushFiles/pushFinanceless.xls").getPath());
while (true) { while (true) {
List<String> sources = new ArrayList<String>();
String path = PushFinanceByTime.class.getClassLoader().getResource("pushFiles/pushFinanceless.xls").getPath();
Document doc = con.find("financePush", "excelPath", new Document().append("name", "lessSource"));
if (doc != null) {
if (doc.getString("url") != null) {
path = doc.getString("url");
}
}
log.info("推送检测"); log.info("推送检测");
System.out.println(path);
sources = new ReadExcel().getExcel(path);
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
try { try {
if (cal.get(Calendar.HOUR_OF_DAY) == 8 && cal.get(Calendar.MINUTE) >= 30) { if (cal.get(Calendar.HOUR_OF_DAY) == 8 && cal.get(Calendar.MINUTE) >= 30) {
......
...@@ -19,13 +19,14 @@ public class Services { ...@@ -19,13 +19,14 @@ public class Services {
List<ExcelsUrl> result = new ArrayList<>(); List<ExcelsUrl> result = new ArrayList<>();
for (Document doc : list) { for (Document doc : list) {
ExcelsUrl eUrl = new ExcelsUrl(); ExcelsUrl eUrl = new ExcelsUrl();
eUrl.setType(doc.getString("type"));
eUrl.setName(doc.getString("name")); eUrl.setName(doc.getString("name"));
eUrl.setUrl(doc.getString("url")); eUrl.setUrl(doc.getString("url"));
switch (doc.getString("name")) { // switch (doc.getString("name")) {
case "key":eUrl.setGo("http://192.168.0.245:8080/change4weixin/finance/upExcel");; // case "key":eUrl.setGo("http://192.168.0.245:8080/change4weixin/finance/upExcel");;
case "longSource":eUrl.setGo("http://192.168.0.245:8080/change4weixin/finance/upExcel"); // case "longSource":eUrl.setGo("http://192.168.0.245:8080/change4weixin/finance/upExcel");
case "lessSource":eUrl.setGo("http://192.168.0.245:8080/change4weixin/finance/upExcel"); // case "lessSource":eUrl.setGo("http://192.168.0.245:8080/change4weixin/finance/upExcel");
} // }
result.add(eUrl); result.add(eUrl);
} }
return result; return result;
...@@ -33,8 +34,10 @@ public class Services { ...@@ -33,8 +34,10 @@ public class Services {
public void insert(ExcelsUrl eUrl) { public void insert(ExcelsUrl eUrl) {
Document doc = new Document(); Document doc = new Document();
System.out.println(eUrl);
doc.append("name", eUrl.getName()); doc.append("name", eUrl.getName());
doc.append("url", eUrl.getUrl()); doc.append("url", eUrl.getUrl());
doc.append("type", eUrl.getType());
con.insertDb("financePush", "excelPath", doc); con.insertDb("financePush", "excelPath", doc);
} }
......
package com.zhiwei.servlet; package com.zhiwei.servlet;
import java.io.File;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
...@@ -16,6 +17,7 @@ import com.alibaba.fastjson.JSON; ...@@ -16,6 +17,7 @@ import com.alibaba.fastjson.JSON;
import com.zhiwei.service.Services; import com.zhiwei.service.Services;
import com.zhiwei.util.Config; import com.zhiwei.util.Config;
import com.zhiwei.util.ExcelUtil; import com.zhiwei.util.ExcelUtil;
import com.zhiwei.util.ReadExcel;
import com.zhiwei.util.StringUtil; import com.zhiwei.util.StringUtil;
import com.zw.entity.ExcelsUrl; import com.zw.entity.ExcelsUrl;
...@@ -34,24 +36,39 @@ public class FianceController { ...@@ -34,24 +36,39 @@ public class FianceController {
return "showfinance"; return "showfinance";
} }
public void fileSave(MultipartFile file, String path) {
File targetFile = new File(path);
if (!targetFile.exists()) {
targetFile.mkdirs();
}
// 保存
try {
file.transferTo(targetFile);
} catch (Exception e) {
e.printStackTrace();
}
}
@RequestMapping(value = "/upFinanceKey") @RequestMapping(value = "/upFinanceKey")
public String upFinanceKey(MultipartFile file) { public String upFinanceKey(MultipartFile file) {
String fileName = file.getOriginalFilename(); String fileName = file.getOriginalFilename();
if (fileName.endsWith(".xls") || fileName.endsWith(".xlsx")) {// 是否是excel结尾的 if (fileName.endsWith(".xls") || fileName.endsWith(".xlsx")) {// 是否是excel结尾的
String path = Config.getVal("timefileUrl") + "/" + sdf.format(new Date()) + fileName; String path = Config.getVal("timefileUrl") + "/" + sdf.format(new Date()) + fileName;
log.info(path); log.info(path);
String beforeUrl=service.getPath("key"); String beforeUrl = service.getPath("key");
if(!StringUtil.isEmpty(beforeUrl)) { if (!StringUtil.isEmpty(beforeUrl)) {
ExcelUtil.delete(beforeUrl); ExcelUtil.delete(beforeUrl);
} }
fileSave(file, path);
ExcelsUrl eUrl = new ExcelsUrl(); ExcelsUrl eUrl = new ExcelsUrl();
eUrl.setType("关键词");
eUrl.setName("key"); eUrl.setName("key");
eUrl.setUrl(path); eUrl.setUrl(path);
service.insert(eUrl); service.insert(eUrl);
} else { } else {
return ""; return "excelError";
} }
return "upExcelWithFinance"; return "showfinance";
} }
@RequestMapping(value = "/upFinanceLong") @RequestMapping(value = "/upFinanceLong")
...@@ -60,18 +77,20 @@ public class FianceController { ...@@ -60,18 +77,20 @@ public class FianceController {
if (fileName.endsWith(".xls") || fileName.endsWith(".xlsx")) {// 是否是excel结尾的 if (fileName.endsWith(".xls") || fileName.endsWith(".xlsx")) {// 是否是excel结尾的
String path = Config.getVal("timefileUrl") + "/" + sdf.format(new Date()) + fileName; String path = Config.getVal("timefileUrl") + "/" + sdf.format(new Date()) + fileName;
log.info(path); log.info(path);
String beforeUrl=service.getPath("longSource"); String beforeUrl = service.getPath("longSource");
if(!StringUtil.isEmpty(beforeUrl)) { if (!StringUtil.isEmpty(beforeUrl)) {
ExcelUtil.delete(beforeUrl); ExcelUtil.delete(beforeUrl);
} }
fileSave(file, path);
ExcelsUrl eUrl = new ExcelsUrl(); ExcelsUrl eUrl = new ExcelsUrl();
eUrl.setType("来源多的文件");
eUrl.setName("longSource"); eUrl.setName("longSource");
eUrl.setUrl(path); eUrl.setUrl(path);
service.insert(eUrl); service.insert(eUrl);
} else { } else {
return ""; return "excelError";
} }
return "upExcelWithFinance"; return "showfinance";
} }
@RequestMapping(value = "/upFinanceLess") @RequestMapping(value = "/upFinanceLess")
...@@ -80,25 +99,26 @@ public class FianceController { ...@@ -80,25 +99,26 @@ public class FianceController {
if (fileName.endsWith(".xls") || fileName.endsWith(".xlsx")) {// 是否是excel结尾的 if (fileName.endsWith(".xls") || fileName.endsWith(".xlsx")) {// 是否是excel结尾的
String path = Config.getVal("timefileUrl") + "/" + sdf.format(new Date()) + fileName; String path = Config.getVal("timefileUrl") + "/" + sdf.format(new Date()) + fileName;
log.info(path); log.info(path);
String beforeUrl=service.getPath("lessSource"); String beforeUrl = service.getPath("lessSource");
if(!StringUtil.isEmpty(beforeUrl)) { if (!StringUtil.isEmpty(beforeUrl)) {
ExcelUtil.delete(beforeUrl); ExcelUtil.delete(beforeUrl);
} }
fileSave(file, path);
ExcelsUrl eUrl = new ExcelsUrl(); ExcelsUrl eUrl = new ExcelsUrl();
eUrl.setType("来源较少的文件");
eUrl.setName("lessSource"); eUrl.setName("lessSource");
eUrl.setUrl(path); eUrl.setUrl(path);
service.insert(eUrl); service.insert(eUrl);
} else { } else {
return "excelError"; return "excelError";
} }
return "upExcelWithFinance"; return "redirect:upExcel";
} }
@RequestMapping(value="/getAll") @RequestMapping(value = "/getAll", produces = "application/json;charset=utf-8")
@ResponseBody @ResponseBody
public String getAll() { public String getAll() {
return JSON.toJSONString(service.findAll()); return JSON.toJSONString(service.findAll());
} }
} }
...@@ -34,6 +34,7 @@ public class MongoConnection { ...@@ -34,6 +34,7 @@ public class MongoConnection {
MongoCollection<Document> col = db.getCollection(collection); MongoCollection<Document> col = db.getCollection(collection);
Document filter = new Document(); Document filter = new Document();
filter.append("name", document.get("name")); filter.append("name", document.get("name"));
filter.append("type", document.get("type"));
Document update = new Document(); Document update = new Document();
update.append("$set", new Document("url",document.get("url"))); update.append("$set", new Document("url",document.get("url")));
col.updateOne(filter, update, new UpdateOptions().upsert(true)); col.updateOne(filter, update, new UpdateOptions().upsert(true));
...@@ -57,6 +58,13 @@ public class MongoConnection { ...@@ -57,6 +58,13 @@ public class MongoConnection {
cur.close(); cur.close();
return list; return list;
} }
public Document find(String dbName, String collection,Document doc) {
MongoDatabase db = getdb(dbName);
MongoCollection<Document> col = db.getCollection(collection);
Document fid = col.find(doc).first();
return fid;
}
/** /**
* 条件查询 * 条件查询
* @param dbName * @param dbName
......
...@@ -3,6 +3,13 @@ package com.zw.entity; ...@@ -3,6 +3,13 @@ package com.zw.entity;
public class ExcelsUrl { public class ExcelsUrl {
private String url; private String url;
private String type;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
private String name; private String name;
private String go; private String go;
public String getGo() { public String getGo() {
...@@ -25,6 +32,7 @@ public class ExcelsUrl { ...@@ -25,6 +32,7 @@ public class ExcelsUrl {
} }
@Override @Override
public String toString() { public String toString() {
return "ExcelsUrl [url=" + url + ", name=" + name + "]"; return "ExcelsUrl [url=" + url + ", type=" + type + ", name=" + name + ", go=" + go + "]";
} }
} }
...@@ -21,8 +21,6 @@ import javax.mail.internet.MimeMultipart; ...@@ -21,8 +21,6 @@ import javax.mail.internet.MimeMultipart;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
/** /**
* 閭欢鍙戯拷?锟藉伐鍏风被 * 閭欢鍙戯拷?锟藉伐鍏风被
* *
...@@ -30,16 +28,17 @@ import org.apache.commons.logging.LogFactory; ...@@ -30,16 +28,17 @@ import org.apache.commons.logging.LogFactory;
*/ */
public class SendMailUtil { public class SendMailUtil {
private final static Log log=LogFactory.getLog(SendMailUtil.class); private final static Log log = LogFactory.getLog(SendMailUtil.class);
// 发送邮件的邮箱号(若有授权码则密码输入授权码) // 发送邮件的邮箱号(若有授权码则密码输入授权码)
// public static String myEmailAccount = "ccxe4zhishu@126.com"; public static String myEmailAccount2 = "ccxe4zhishu@126.com";
// public static String myEmailPassword = "1q2w3e4r5t"; public static String myEmailPassword2 = "1q2w3e4r5t";
public static String myEmailAccount = "ccxerisk@sina.com"; public static String myEmailAccount = "ccxerisk@sina.com";
public static String myEmailPassword = "1q2w3e4r"; public static String myEmailPassword = "1q2w3e4r";
// 发件人邮箱的 SMTP 服务器地址, 必须准确, 不同邮件服务器地址不同, 一般(只是一般, 绝非绝对)格式为: smtp.xxx.com // 发件人邮箱的 SMTP 服务器地址, 必须准确, 不同邮件服务器地址不同, 一般(只是一般, 绝非绝对)格式为: smtp.xxx.com
// 网易163邮箱的 SMTP 服务器地址为: smtp.163.com // 网易163邮箱的 SMTP 服务器地址为: smtp.163.com
public static String myEmailSMTPHost = "smtp.sina.com"; public static String myEmailSMTPHost = "smtp.sina.com";
public static String myEmailSMTPHost2 = "smtp.126.com";
/** /**
* *
...@@ -47,9 +46,10 @@ public class SendMailUtil { ...@@ -47,9 +46,10 @@ public class SendMailUtil {
* 接收者的邮箱号 * 接收者的邮箱号
* @param url * @param url
* 本地的附件地址 * 本地的附件地址
* @throws Exception
*/ */
public static void sendMessage(String mail, String url,String str) { public static void sendMessage(String mail, String url, String str) throws Exception {
log.info(mail+","+url+","+str); log.info(mail + "," + url + "," + str);
// 创建参数配置, 用于连接邮件服务器的参数配置 // 创建参数配置, 用于连接邮件服务器的参数配置
Properties props = new Properties(); Properties props = new Properties();
// 使用的协议(JavaMail规范要求) // 使用的协议(JavaMail规范要求)
...@@ -59,50 +59,44 @@ public class SendMailUtil { ...@@ -59,50 +59,44 @@ public class SendMailUtil {
// 需要请求认证 // 需要请求认证
props.setProperty("mail.smtp.auth", "true"); props.setProperty("mail.smtp.auth", "true");
final String smtpPort = "465"; final String smtpPort = "465";
props.setProperty("mail.smtp.port", smtpPort); props.setProperty("mail.smtp.port", smtpPort);
props.setProperty("mail.smtp.socketFactory.class", props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
"javax.net.ssl.SSLSocketFactory"); props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.socketFactory.fallback", "false"); props.setProperty("mail.smtp.socketFactory.port", smtpPort);
props.setProperty("mail.smtp.socketFactory.port", smtpPort);
// PopupAuthenticator popAuthenticator=new PopupAuthenticator();
// PasswordAuthentication pop = popAuthenticator.performCheck("wanghongshuai@ccxe.com.cn","tt67z890zh");
// Session mysession = Session.getInstance(props, popAuthenticator);
//
// PS: 某些邮箱服务器要求 SMTP 连接需要使用 SSL 安全认证 (为了提高安全性, 邮箱支持SSL连接, 也可以自己开启),
// 如果无法连接邮件服务器, 仔细查看控制台打印的 log, 如果有有类似 “连接失败, 要求 SSL 安全连接” 等错误,
/*
* // SMTP 服务器的端口 (非 SSL 连接的端口一般默认为 25, 可以不添加, 如果开启了 SSL 连接, //
* 需要改为对应邮箱的 SMTP 服务器的端口, 具体可查看对应邮箱服务的帮助, // QQ邮箱的SMTP(SLL)端口为465或587,
* 其他邮箱自行去查看) final String smtpPort = "465";
* props.setProperty("mail.smtp.port", smtpPort);
* props.setProperty("mail.smtp.socketFactory.class",
* "javax.net.ssl.SSLSocketFactory");
* props.setProperty("mail.smtp.socketFactory.fallback", "false");
* props.setProperty("mail.smtp.socketFactory.port", smtpPort);
*/
// 根据配置创建会话对象, 用于和邮件服务器交互
Session session = Session.getDefaultInstance(props); Session session = Session.getDefaultInstance(props);
session.setDebug(false); // 设置为debug模式, 可以查看详细的发送 log session.setDebug(false); // 设置为debug模式, 可以查看详细的发送 log
// 创建邮件
MimeMessage message; MimeMessage message;
try { message = createMimeMessage(session, myEmailAccount, mail, url, str);
message = createMimeMessage(session, myEmailAccount, mail, url,str); Transport transport = session.getTransport();
// 根据 Session 获取邮件传输对象 transport.connect(myEmailAccount, myEmailPassword);
Transport transport = session.getTransport(); transport.sendMessage(message, message.getAllRecipients());
/* transport.close();
* 使用 邮箱账号 和 密码 连接邮件服务器, 这里认证的邮箱必须与 message 中的发件人邮箱一致, 否则报错 PS_02: }
* 连接失败的原因通常为以下几点, 仔细检查代码: (1) 邮箱没有开启 SMTP 服务; (2) 邮箱密码错误,
* 例如某些邮箱开启了独立密码; (3) 邮箱服务器要求必须要使用 SSL 安全连接; (4) 请求过于频繁或其他原因,
* 被邮件服务器拒绝服务; (5) 如果以上几点都确定无误, 到邮件服务器网站查找帮助。 public static void sendMessage2(String mail, String url, String str) throws Exception {
*/ log.info(mail + "," + url + "," + str);
transport.connect(myEmailAccount, myEmailPassword); // 创建参数配置, 用于连接邮件服务器的参数配置
// 发送邮件 Properties props = new Properties();
transport.sendMessage(message, message.getAllRecipients()); // 使用的协议(JavaMail规范要求)
// 关闭连接 props.setProperty("mail.transport.protocol", "smtp");
transport.close(); // 发件人的邮箱的 SMTP 服务器地址
} catch (Exception e) { props.setProperty("mail.smtp.host", myEmailSMTPHost2);
e.printStackTrace(); // 需要请求认证
} props.setProperty("mail.smtp.auth", "true");
final String smtpPort = "465";
props.setProperty("mail.smtp.port", smtpPort);
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.socketFactory.port", smtpPort);
Session session = Session.getDefaultInstance(props);
session.setDebug(false); // 设置为debug模式, 可以查看详细的发送 log
MimeMessage message;
message = createMimeMessage(session, myEmailAccount2, mail, url, str);
Transport transport = session.getTransport();
transport.connect(myEmailAccount2, myEmailPassword2);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} }
/** /**
...@@ -119,28 +113,18 @@ public class SendMailUtil { ...@@ -119,28 +113,18 @@ public class SendMailUtil {
* @return * @return
* @throws Exception * @throws Exception
*/ */
public static MimeMessage createMimeMessage(Session session, String sendMail, String receiveMail, String url,String strtitle) public static MimeMessage createMimeMessage(Session session, String sendMail, String receiveMail, String url,
throws Exception { String strtitle) throws Exception {
// 创建邮件 // 创建邮件
MimeMessage message = new MimeMessage(session); MimeMessage message = new MimeMessage(session);
SimpleDateFormat sfm = new SimpleDateFormat("yyyyMMdd"); SimpleDateFormat sfm = new SimpleDateFormat("yyyyMMdd");
String str = sfm.format(new Date()); String str = sfm.format(new Date());
// From: 发件人
message.setFrom(new InternetAddress(sendMail, "有新短消息", "UTF-8")); message.setFrom(new InternetAddress(sendMail, "有新短消息", "UTF-8"));
// To: 收件人(可以增加多个收件人、抄送、密送)
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail, "UTF-8")); message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail, "UTF-8"));
// Subject: 邮件主题
message.setSubject(str + "数据结果", "UTF-8"); message.setSubject(str + "数据结果", "UTF-8");
/*
* Content: 邮件正文(可以使用html标签) message.setContent(
* "XX用户你好, 今天全场5折, 快来抢购, 错过今天再等一年。。。", "text/html;charset=UTF-8");
*/
// 创建消息部分
BodyPart messageBodyPart = new MimeBodyPart(); BodyPart messageBodyPart = new MimeBodyPart();
String content = "你好"; String content = "你好";
// content=new MailContent().getHtml(); messageBodyPart.setContent(content, "text/html; charset=utf-8");
// 正文消息消息
messageBodyPart.setContent(content,"text/html; charset=utf-8");
// 创建多重消息 // 创建多重消息
Multipart multipart = new MimeMultipart(); Multipart multipart = new MimeMultipart();
// 设置文本消息部分 // 设置文本消息部分
...@@ -152,29 +136,20 @@ public class SendMailUtil { ...@@ -152,29 +136,20 @@ public class SendMailUtil {
DataSource source = new FileDataSource(filename); DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setDataHandler(new DataHandler(source));
String hourstr = ""; String hourstr = "";
if (new Date().getHours() <= 10) {
hourstr = "09";
} else if(new Date().getHours()<=13){
hourstr = "12";
} else{
hourstr = "16";
}
// 年月日时
messageBodyPart.setFileName(sfm.format(new Date()) + hourstr + ".xls"); messageBodyPart.setFileName(sfm.format(new Date()) + hourstr + ".xls");
multipart.addBodyPart(messageBodyPart); multipart.addBodyPart(messageBodyPart);
// 发送完整消息
message.setContent(multipart); message.setContent(multipart);
// 设置发件时间
message.setSentDate(new Date()); message.setSentDate(new Date());
// 保存设置
message.saveChanges(); message.saveChanges();
return message; return message;
} }
public static void main(String[] args) {
SendMailUtil.sendMessage("shenjinzhu999@163.com", "C:/Users/Administrator/Desktop/zhishu.xls","ccxe"); public static void main(String[] args) {
} try {
SendMailUtil.sendMessage("shenjinzhu999@163.com", "C:/Users/Administrator/Desktop/zhishu.xls", "ccxe");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} }
basefile=/home/nbzhiwei/upload/ basefile=/home/nbzhiwei/upload/
#basefile=C:/Users/Administrator/Desktop/news222.xls #basefile=C:/Users/Administrator/Desktop/news222.xls
timefileUrl=/home/nbzhiwei/upload timefileUrl=/home/nbzhiwei/upload
#timefileUrl=C:/Users/Administrator/Desktop #timefileUrl=C:/Users/admin/Desktop
#host=106.14.249.240 #host=106.14.249.240
#port=26807 #port=26807
#password=1q2w3e4r #password=1q2w3e4r
...@@ -22,3 +22,5 @@ type=media ...@@ -22,3 +22,5 @@ type=media
openid=oTc_rssM56GjwdRgwTD2dfQMq4Rg openid=oTc_rssM56GjwdRgwTD2dfQMq4Rg
#,oTc_rsju0ZJsEnzFX3WRgZihNWpc,oTc_rsme_NjN1tVh3GSHLdHtq1o8 #,oTc_rsju0ZJsEnzFX3WRgZihNWpc,oTc_rsme_NjN1tVh3GSHLdHtq1o8
########################################
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
</head> </head>
<body> <body>
<a href="/change4weixin/upExcelWithFinance.html">文件上传页</a>
<div id="app"> <div id="app">
<table class="table table-bordered"> <table class="table table-bordered">
<thead> <thead>
...@@ -32,9 +33,10 @@ ...@@ -32,9 +33,10 @@
</thead> </thead>
<tbody> <tbody>
<tr v-for="el in tbodyList"> <tr v-for="el in tbodyList">
<td>{{el.type}}</td>
<td>{{el.name}}</td> <td>{{el.name}}</td>
<td>{{el.url}}</td> <td>{{el.url}}</td>
<td @click="() => window.location.href = el.go">点击上传</td> <td><a href="/change4weixin/upExcelWithFinance.html">上传页</a></td>
</tr> </tr>
</tbody> </tbody>
</table> </table>
...@@ -47,7 +49,7 @@ ...@@ -47,7 +49,7 @@
new Vue({ new Vue({
el: '#app', el: '#app',
data: { data: {
headList: ['文件', '地址', '上传'], headList: ['文件类型','文件', '地址', '上传页'],
tbodyList: [] tbodyList: []
}, },
created() { created() {
......
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
<style type="text/css"> <style type="text/css">
body { body {
background-image: url("../2.jpg"); background-image: url("../change4weixin/2.jpg");
display: flex; display: flex;
flex-flow: column nowrap; flex-flow: column nowrap;
justify-content: center; justify-content: center;
...@@ -74,14 +74,14 @@ form { ...@@ -74,14 +74,14 @@ form {
<input type="submit" value="提交" class="form-control" id="submit" /> <input type="submit" value="提交" class="form-control" id="submit" />
</div> </div>
</form> </form>
<form action="/change4weixin/finance/upFinanceSource" enctype="multipart/form-data" <form action="/change4weixin/finance/upFinanceLong" enctype="multipart/form-data"
method="post"> method="post">
<span>长来源文件</span> <input type="file" name="file" /> <span>长来源文件</span> <input type="file" name="file" />
<div class="form-group"> <div class="form-group">
<input type="submit" value="提交" class="form-control" id="submit" /> <input type="submit" value="提交" class="form-control" id="submit" />
</div> </div>
</form> </form>
<form action="/change4weixin/finance/upFinanceSourceLess" enctype="multipart/form-data" <form action="/change4weixin/finance/upFinanceLess" enctype="multipart/form-data"
method="post"> method="post">
<span>短来源文件</span> <input type="file" name="file" /> <span>短来源文件</span> <input type="file" name="file" />
<div class="form-group"> <div class="form-group">
......
basefile=/home/nbzhiwei/upload/ basefile=/home/nbzhiwei/upload/
#basefile=C:/Users/Administrator/Desktop/news222.xls #basefile=C:/Users/Administrator/Desktop/news222.xls
timefileUrl=/home/nbzhiwei/upload timefileUrl=/home/nbzhiwei/upload
#timefileUrl=C:/Users/Administrator/Desktop #timefileUrl=C:/Users/admin/Desktop
#host=106.14.249.240 #host=106.14.249.240
#port=26807 #port=26807
#password=1q2w3e4r #password=1q2w3e4r
...@@ -22,3 +22,5 @@ type=media ...@@ -22,3 +22,5 @@ type=media
openid=oTc_rssM56GjwdRgwTD2dfQMq4Rg openid=oTc_rssM56GjwdRgwTD2dfQMq4Rg
#,oTc_rsju0ZJsEnzFX3WRgZihNWpc,oTc_rsme_NjN1tVh3GSHLdHtq1o8 #,oTc_rsju0ZJsEnzFX3WRgZihNWpc,oTc_rsme_NjN1tVh3GSHLdHtq1o8
########################################
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment