我正在尝试阅读xml文件并尝试发送邮件。
这是xml代码
<?xml version="1.0" encoding="UTF-8"?><xml>
<u_incident_task>
<description>messafe</description>
<priority>1</priority>
<number>12345</number>
<u_task_incident_service_ci>A</u_task_incident_service_ci>
</u_incident_task>
<u_incident_task>
<description>messafe</description>
<priority>3</priority>
<number>123456</number>
<u_task_incident_service_ci>A</u_task_incident_service_ci>
</u_incident_task>
</xml>
所以,当优先级为1时,我需要发送邮件。目前我的下面代码正在工作并且能够发送。 但是当优先级从3更新为1时,邮件无法发送。可能是我将所有数字存储在数组中的原因,因为我不想重复发送邮件。所以我将数字存储在数组中,这样我只能发送一次。所以当我存储数字时,如果任何优先级更新为1邮件不起作用。
但是当我发送1封邮件创建记录但是如果我们将现有优先级从3更新为1则无法正常工作。完整的工作代码在这里。任何人都可以帮助我在逻辑中错过的东西吗?
这里有完整的代码。 Incidentreport2000.java
package com.asn.model;
import java.awt.Desktop;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.asn.model.incident200;
import com.asn.model.incident1;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import javax.mail.Message;
import javax.mail.MessagingException;import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
public class incidentreport2000 implements Runnable {
List<String> number1 = new ArrayList<String>();
public incidentreport2000(){
}
public void run(){
for(int i=1;i>0;i++){
try{
Thread.sleep(9600);
List<incident1> incident1List = new ArrayList<incident1>();
List<incident200> incident200List = new ArrayList<incident200>();
FileWriter fw =null;
BufferedWriter bw=null;
String Path = "C:\\Users";
OutputStream out = null;
URLConnection conn = null;
InputStream in = null;
InputStream input = null;
String address ="C:\\Users" ;
String localFileName = "\\file.xml";
try {
File fXmlFile = new File(Path + "\\file.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList incident1NodeList = doc.getElementsByTagName("u_incident_task");
for (int temp = 0; temp < incident1NodeList.getLength(); temp++) {
Node varNode = incident1NodeList.item(temp);
if (varNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) varNode;
NodeList u_task_incident_service_ciList = eElement.getElementsByTagName
("u_task_incident_service_ci");
NodeList varsionList = eElement.getElementsByTagName("priority");
NodeList numberList = eElement.getElementsByTagName("number");
NodeList descriptionList = eElement.getElementsByTagName("description");
/* Node u_task_incident_service_ciNode =
u_task_incident_service_ciList.item(0);
Node priorityNode = varsionList.item(0);*/
Node u_task_incident_service_ciNode =
u_task_incident_service_ciList.item(0);
Node priorityNode = varsionList.item(0);
Node numberNode = numberList.item(0);
Node descriptionNode = descriptionList.item(0);
if (u_task_incident_service_ciNode.getNodeType() == Node.ELEMENT_NODE
&& priorityNode.getNodeType() ==
Node.ELEMENT_NODE) {
Element u_task_incident_service_ciElement = (Element)
u_task_incident_service_ciNode;
Element priorityElement = (Element) priorityNode;
Element numberElement = (Element) numberNode;
Element descriptionElement = (Element) descriptionNode;
incident1 incident1 = new incident1(
u_task_incident_service_ciElement.getTextContent(),
priorityElement.getTextContent());
incident1List.add(incident1);
incident200 incident200 = new incident200(
u_task_incident_service_ciElement.getTextContent(),
priorityElement.getTextContent(),numberElement.getTextContent (),descriptionElement.getTextContent());
incident200List.add(incident200);
}
}
// fw = new FileWriter(file.getAbsoluteFile());
// bw = new BufferedWriter(fw);
}
} catch (Exception e) {
e.printStackTrace();
}
Map<incident1, Integer> count = new HashMap<incident1, Integer>();
for (incident1 c : incident1List)
if (!count.containsKey(c))
count.put(c, Collections.frequency(incident1List, c));
List<String> prioritys = getprioritys(count);
List<String> u_task_incident_service_cis = getu_task_incident_service_cis(count);
Map<incident200, Integer> count1 = new HashMap<incident200, Integer>();
for (incident200 c1 : incident200List)
if (!count1.containsKey(c1))
count1.put(c1, Collections.frequency(incident200List, c1));
List<String> number = getnumber(count1);
List<String> description = getdescription(count1);
List<String> prioritys1 = getprioritys1(count1);
List<String> u_task_incident_service_cis1 = getu_task_incident_service_cis1(count1);
for (String v2 : prioritys1 ) {
if (v2 =="P1" ){
for (String n2 : number ) {
System.out.println(number);
if (!number1.contains(n2)) {
for (String d : description){
for (String m3 : u_task_incident_service_cis1){
if (m3 == "A"){
getCountu_task_incident_service_cis1( m3,prioritys1,n2,d, count1);
}
if (m3 == "B"){
getCountu_task_incident_service_cis1( m3,prioritys1,n2,d, count1);
}
}} number1.add(n2);
}}
}}
}catch(Exception e){}
}
}
private static List<String> getu_task_incident_service_cis(Map<incident1, Integer>
count) {
List<String> u_task_incident_service_cis = new ArrayList<String>();
for (Map.Entry<incident1, Integer> ent : count.entrySet())
if (!u_task_incident_service_cis.contains(ent.getKey
().getu_task_incident_service_ci()))
u_task_incident_service_cis.add(ent.getKey().getu_task_incident_service_ci
());
System.out.println(u_task_incident_service_cis);
if(!u_task_incident_service_cis.contains("A"))
u_task_incident_service_cis.add("A");
return u_task_incident_service_cis;
}
private static List<String> getu_task_incident_service_cis1(Map<incident200, Integer>
count) {
List<String> u_task_incident_service_cis1 = new ArrayList<String>();
for (Map.Entry<incident200, Integer> ent : count.entrySet())
if (!u_task_incident_service_cis1.contains(ent.getKey
().getu_task_incident_service_ci()))
u_task_incident_service_cis1.add(ent.getKey().getu_task_incident_service_ci
());
System.out.println(u_task_incident_service_cis1+"NewCIS");
if(!u_task_incident_service_cis1.contains("BIRSD"))
u_task_incident_service_cis1.add("BIRSD");
return u_task_incident_service_cis1;
}
private static String getNumOfu_task_incident_service_cis(String
u_task_incident_service_ci, List<String> prioritys,
Map<incident1, Integer>
count) {
StringBuilder builder = new StringBuilder();
for (String v : prioritys) {
Integer cnt = count.get(new incident1(u_task_incident_service_ci, v));
if (cnt == null) {
cnt = 0;
}
}
return builder.toString();
}
private static String getCountu_task_incident_service_cis1(String
u_task_incident_service_ci, List<String> prioritys1, String number,String description,
Map<incident200, Integer>
count1) {
StringBuilder builder1 = new StringBuilder();
List<String> ARRAY = new ArrayList<>();
for (String v : prioritys1) {
if ( v == "P1") {
Integer cnt1 = count1.get(new incident200(u_task_incident_service_ci, v,number,description));
if (cnt1 == null) {
cnt1 = 0;
}
else
if (cnt1 !=0){
cnt1 = 1;
if (!ARRAY.contains(number)) {
mail1 (u_task_incident_service_ci,v,number,description);
ARRAY.add(number);
}
}}
else
if ( v == "P2") {
Integer cnt1 = count1.get(new incident200(u_task_incident_service_ci, v,number,description));
if (cnt1 == null) {
cnt1 = 0;
}
else
if (cnt1 !=0){
if (!ARRAY.contains(number)) {
mail1(u_task_incident_service_ci,v,number,description);
ARRAY.add(number);
}}
}}
return number;
}
private static List<String> getprioritys(Map<incident1, Integer> count) {
List<String> prioritys = new ArrayList<String>();
for (Map.Entry<incident1, Integer> ent : count.entrySet())
if (!prioritys.contains(ent.getKey().getpriority()))
prioritys.add(ent.getKey().getpriority());
Collections.sort(prioritys);
return prioritys;
}
private static List<String> getprioritys1(Map<incident200, Integer> count) {
List<String> prioritys1 = new ArrayList<String>();
for (Map.Entry<incident200, Integer> ent : count.entrySet())
if (!prioritys1.contains(ent.getKey().getpriority()))
prioritys1.add(ent.getKey().getpriority());
;
//Collections.sort(prioritys);
System.out.println("check1");
return prioritys1;
}
private static List<String> getnumber(Map<incident200, Integer> count) {
List<String> number = new ArrayList<String>();
for (Map.Entry<incident200, Integer> ent : count.entrySet())
if (!number.contains(ent.getKey().getnumber()))
number.add(ent.getKey().getnumber());
return number;
}
private static List<String> getdescription(Map<incident200, Integer> count) {
List<String> description = new ArrayList<String>();
for (Map.Entry<incident200, Integer> ent : count.entrySet())
if (!description.contains(ent.getKey().getdescription()))
description.add(ent.getKey().getdescription());
return description;
}
public static void mail1(String Email,String v,String n, String d) {
System.out.println(Email);
System.out.println(v);
final String username = "mailid";
final String password = "password";
//w2011.kpnnl.local
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
//props.put("mail.transport.protocol","smtp");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("from.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse
("toaddress"));
message.setSubject("Priority:"+v+": "+n+"Incident has bee raised for the team"+Email);
message.setText("messager,"
);
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) throws IOException {
incidentreport2000 mrt = new incidentreport2000();
Thread t = new Thread(mrt);
t.start();
String Path = "C:\\Users";
List<String> number1 = new ArrayList<String>();
}
}
incident200.java
package com.asn.model;
public class incident200 {
private String u_task_incident_service_ci;
private String priority;
private String number;
private String description;
public incident200(String u_task_incident_service_ci, String priority, String number,String description ) {
super();
if (u_task_incident_service_ci.equals("A"))
{ String Team= "A";
this.u_task_incident_service_ci = Team;}
else
this.u_task_incident_service_ci = u_task_incident_service_ci;
if (priority.equals("1"))
{ String priority1 = "P1";
this.priority = priority1;}
else
if (priority.equals("3"))
{ String priority3 = "P3";
this.priority = priority3;}
else
if (priority.equals("4"))
{ String priority4 = "P3";
this.priority = priority4;}
else
if (priority.equals("5"))
{ String priority5 = "P3";
this.priority = priority5;}
else
if (priority.equals("2"))
{ String priority2 = "P2";
this.priority = priority2;}
else
this.priority = priority;
this.number = number;
this.description = description;
}
public String getu_task_incident_service_ci() {
return u_task_incident_service_ci;
}
public void setu_task_incident_service_ci(String u_task_incident_service_ci) {
this.u_task_incident_service_ci = u_task_incident_service_ci;
}
public String getpriority() {
return priority;
}
public void setpriority(String priority) {
this.priority = priority;
}
public String getnumber() {
return number;
}
public void setnumber(String number) {
this.number = number;
}
public String getdescription() {
return description;
}
public void setdescription(String description) {
this.description = description;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((u_task_incident_service_ci == null) ? 0 : u_task_incident_service_ci.hashCode());
result = prime * result + ((priority == null) ? 0 : priority.hashCode());
result = prime * result + ((number == null) ? 0 : number.hashCode());
result = prime * result + ((description == null) ? 0 : description.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
incident200 other = (incident200) obj;
if (u_task_incident_service_ci == null) {
if (other.u_task_incident_service_ci != null)
return false;
} else if (!u_task_incident_service_ci.equals(other.u_task_incident_service_ci))
return false;
if (priority == null) {
if (other.priority != null)
return false;
} else if (!priority.equals(other.priority))
return false;
if (number == null) {
if (other.number != null)
return false;
} else if (!number.equals(other.number))
return false;
if (description == null) {
if (other.description != null)
return false;
} else if (!description.equals(other.description))
return false;
return true;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("incident99 [u_task_incident_service_ci=");
builder.append(u_task_incident_service_ci);
builder.append(", priority1=");
builder.append(priority);
builder.append(", number=");
builder.append(number);
builder.append(", description=");
builder.append(description);
builder.append("]");
return builder.toString();
}
}
Incident1.java
package com.asn.model;
public class incident1 {
private String u_task_incident_service_ci;
private String priority;
public incident1(String u_task_incident_service_ci, String priority) {
super();
if (u_task_incident_service_ci.equals("A"))
{ String Team= "A";
this.u_task_incident_service_ci = Team;}
else
this.u_task_incident_service_ci = u_task_incident_service_ci;
if (priority.equals("1"))
{ String priority1 = "P1";
this.priority = priority1;}
else
if (priority.equals("3"))
{ String priority3 = "P3";
this.priority = priority3;}
else
this.priority = priority;
}
public String getu_task_incident_service_ci() {
return u_task_incident_service_ci;
}
public void setu_task_incident_service_ci(String u_task_incident_service_ci) {
this.u_task_incident_service_ci = u_task_incident_service_ci;
}
public String getpriority() {
return priority;
}
public void setpriority(String priority) {
this.priority = priority;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((u_task_incident_service_ci == null) ? 0 : u_task_incident_service_ci.hashCode());
result = prime * result + ((priority == null) ? 0 : priority.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
incident1 other = (incident1) obj;
if (u_task_incident_service_ci == null) {
if (other.u_task_incident_service_ci != null)
return false;
} else if (!u_task_incident_service_ci.equals(other.u_task_incident_service_ci))
return false;
if (priority == null) {
if (other.priority != null)
return false;
} else if (!priority.equals(other.priority))
return false;
return true;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("incident1 [u_task_incident_service_ci=");
builder.append(u_task_incident_service_ci);
builder.append(", priority=");
builder.append(priority);
builder.append("]");
return builder.toString();
}
}
答案 0 :(得分:0)
这段代码很奇怪。看一种方法:
private static String
getNumOfu_task_incident_service_cis(String u_task_incident_service_ci,
List<String> prioritys,
Map<incident1, Integer> count) {
StringBuilder builder = new StringBuilder();
for (String v : prioritys) {
Integer cnt = count.get(new incident1(u_task_incident_service_ci, v));
if (cnt == null) {
cnt = 0;
}
}
return builder.toString();
}
从Map获得了几个“count”值,但StringBuilder仍然保持不变;该方法始终返回“”。
另一种方法:
private static String
getCountu_task_incident_service_cis1( ..., String number,... ){
//...
if ( v == "P1") {
Integer cnt1 = count1.get(new incident200(u_task_incident_service_ci, v,number,description));
if (cnt1 == null) {
cnt1 = 0;
} else
if (cnt1 != 0){
cnt1 = 1;
//...
return number;
}
此处,cnt1
设置为0或1,但稍后不会在同一if语句中使用。返回值为number
,一个String参数。那么 - 有什么意义呢?
public incident200(String u_task_incident_service_ci,
String priority, String number,String description ) {
super();
if (u_task_incident_service_ci.equals("A")){
String Team= "A";
this.u_task_incident_service_ci = Team;}
else
this.u_task_incident_service_ci = u_task_incident_service_ci;
这种并发症的目的是什么?单一陈述就足够了:
this.u_task_incident_service_ci = u_task_incident_service_ci;
我很遗憾我只能指出为什么这段代码难以理解。清理代码可能是找到错误的良好开端。
<强>后来强>
如果方法getCountu_task_incident_service_cis1
应该发送电子邮件,则仅针对优先级P1和P2执行此操作:
for (String v : prioritys1) {
if ( v == "P1") {
// ...
} else
if ( v == "P2") {
//...
}
}
此外,方法run
包含
for (String v2 : prioritys1 )
if (v2 =="P1" ){
// some code...
}
}
您是否希望发送任何其他优先级?