2019独角兽企业重金招聘Python工程师标准>>>
为了考虑以后二次开发,和将来的代码增多。调用工作流的接口,大量代码写在自己新建项目中。
工作流接口:
public boolean sendMail(Map lhm){
//设置HTTP连接的URL地址,就是第二个应用的URL。如果在同一个计算机上可以将192.168.1.134变成127.0.0.1或者localhost
try {
String http="http://localhost:8080/SDKFlow/email.action";
URL url=new URL(http);
//生成HttpURLConnection连接
HttpURLConnection httpurlconnection=(HttpURLConnection) url.openConnection();
//设置有输出流,默认为false,就是不能传递参数。
httpurlconnection.setDoOutput(true);
//设置发送请求的方式。注意:一定要大写
httpurlconnection.setRequestMethod("POST");
//设置连接超时的时间。不过不设置,在网络异常的情况下,可能会造成程序僵死而无法继续向下执行,所以一般设置一个超时时间。单位为毫秒
httpurlconnection.setConnectTimeout(30000);
//设置输出流。
OutputStreamWriter writer=new OutputStreamWriter(httpurlconnection.getOutputStream(), "utf-8");
//传递的参数,中间使用&符号分割。
writer.write("authid="+lhm.get("authid")+"&title="+lhm.get("title"));
//用于刷新缓冲流。因为默认她会写入到内存的缓冲流中,到一定的数据量时,才会写入,使用这个命令可以让他立即写入,不然下面就到关闭流了
writer.flush();
//用于关闭输出流,关闭之后就不可以输出数据了,所以要使用flush刷新缓冲流
writer.close();
//获得返回的请求吗。
int responseCode=httpurlconnection.getResponseCode();
//表示请求成功
if(responseCode==HttpURLConnection.HTTP_OK){
System.out.println("OK"+responseCode);
//获得服务端的输出流。得到返回的数据
InputStream urlstream=httpurlconnection.getInputStream();
BufferedReader reader=new BufferedReader(new InputStreamReader(urlstream));
String line;
String tline="";
while((line=reader.readLine())!=null){
tline+=line;
}
//输出所有的数据
System.out.println(tline);
return true;
}else{
System.out.println("ERR"+responseCode);
}
} catch (Exception e) {
// TODO: handle exception
}
return false;
}
自己新建的项目为spring+mybatis构成。
controller类:
@RequestMapping(value="/email")
public String Email(HttpServletRequest request, HttpServletResponse response)throws Exception{
logger.info("进入邮件控制层");
DataSourceContextHolder.setDbType("horizon");
String authid=request.getParameter("authid");
String title=request.getParameter("title");
// String authid="HZ8080815fe7c0a7015febef32b10180";
// String title="ss";
String str= sdkFlowService.GetUserEmailByUserID(authid,title);
logger.info("返回值为:"+str);
if(str.trim().equals("ok")){
return "welcome";
}else if(str.trim().equals("fail")){
return "";
}else{
return "";
}
}
接口实现类:
/**
* 通过用户信息ID获取用户信息
* @param str 用户ID
* @return 用户相关信息——获取email
* @throws Exception
*/
@Transactional
@Override
public String GetUserEmailByUserID(String id,String title) throws Exception {
logger.info("进入Email接口");
DBUserInfo user = new DBUserInfo();
user.setID(id.trim());
DBUserInfo userifno=sdkflowMapper.getUserEmail(user);
if(userifno.getEMAIL()==null ||userifno.getEMAIL().equals("")){//如果邮箱地址为空,则返回字符串“fail”
return "fail";
}
logger.info("获取下节点人信息:"+userifno);
logger.info("开始发送邮件");
// 邮箱发送邮件
Properties prop = new Properties();
//协议
prop.setProperty("mail.transport.protocol", "smtp");
//服务器
prop.setProperty("mail.smtp.host", "smtp.exmail.qq.com");
//端口
prop.setProperty("mail.smtp.port", "465");
//使用smtp身份验证
prop.setProperty("mail.smtp.auth", "true");
//使用SSL,企业邮箱必需!
//开启安全协议
MailSSLSocketFactory sf = null;
try {
sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
} catch (GeneralSecurityException e1) {
e1.printStackTrace();
}
prop.put("mail.smtp.ssl.enable", "true");
prop.put("mail.smtp.ssl.socketFactory", sf);
//
//获取Session对象
Session s = Session.getDefaultInstance(prop,new Authenticator() {
//此访求返回用户和密码的对象
@Override
protected PasswordAuthentication getPasswordAuthentication() {
PasswordAuthentication pa = new PasswordAuthentication("*************.com", "*******");
return pa;
}
});
//设置session的调试模式,发布时取消
s.setDebug(true);
MimeMessage mimeMessage = new MimeMessage(s);
try {
try {
mimeMessage.setFrom(new InternetAddress("*****.com","*****.com"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(userifno.getEMAIL()));//给指定的人发送邮件
//设置主题
mimeMessage.setSubject(title);
mimeMessage.setSentDate(new Date());
//设置内容
mimeMessage.setText("您有信息需要处理,详情请点击链接http://******/workflow");
mimeMessage.saveChanges();
//发送
Transport.send(mimeMessage);
return "ok";//成功则返回“ok”
} catch (MessagingException e) {
e.printStackTrace();
}
return "fail";
}