Java - 执行concat时字符串值正在中断

时间:2017-12-04 06:47:03

标签: java html spring string concat

我正在使用HTML(我使用spring mime消息发送邮件)准备一个包含嵌入图像的邮件内容。 HTML如下。

<html><body><div><div style='margin-top: 0; margin-bottom: 0;'><font face='Times New Roman' size='3'>&nbsp;</font></div><div style='margin-top: 0; margin-bottom: 0;'><table width='100%' style='border-collapse: collapse; width: 100%;'><tbody><tr height='8' style='height: 8px;'><td style='width: 100%; padding: 0; border: 0 none black;'><table width='100%'style='border-collapse: collapse; width: 100%;'><tbody><tr height='8' style='height: 8px;'><td style='width: 100%; padding: 0; border: 0 none black;'><table width='600' align='center' style='border-collapse: collapse; width: 600px;><tbody><tr height='8' style='height: 8px;'><td width='600' style='width: 600px; padding: 0; border: 0 none black;'><table width='600' align='center'style='border-collapse: collapse; width: 600px;'><tbody><tr height='8' style='height: 8px;'><td width='600'style='width: 600px; padding: 0; border: 0 none black;'><table width='600' align='left'style='border-collapse: collapse; width: 600px;'><tbody><tr height='8' style='height: 8px;'><td width='600' style='width: 600px; padding: 0;border: 0 none black;'><table width='600' align='left'style='border-collapse:collapse; width: 600px;'><tbody><tr height='8' style='height: 8px;'><td width='600'style='width:600px; padding:0; border: 0 none black;'><div align='center' style='text-align:center;'><a href='http://www.e-XXXXbank.com/'target='_blank'> <img src='cid:logo' 'align='top' alt='logo' style='display:inline;'></a></div></td></tr></tbody></table></td></tr></tbody></table></td></tr></tbody></table></td></tr></tbody></table></td></tr></tbody></table></td></tr><tr height='8' style='height: 8px;'><td style='padding: 0; border: 0 none black;'><br><table width='600' align='center'<tbody><tr height='8' style='height: 8px;'><td width='600'style='width: 600px; padding: 0; border: 0 none black;'><table width='560' align='center'style='border-collapse: collapse; width: 560px;'><tbody><tr height='8' style='height: 8px;'><td width='560'style='width: 560px; padding: 0; border: 0 none black;'><table width='100%'style='border-collapse: collapse; width: 100%;'><tbody><tr height='8' style='height: 8px;'><td style='width: 100%; padding: 0; border: 0 none black;'><table width='560' align='center'style='border-collapse: collapse; width: 560px;'><tbody><tr height='8' style='height: 8px;'><td width='558'style='width: 558px; background-color: #F0F0F0; padding: 0; border: 1px solid #ECECED;'><span style='background-color: #F0F0F0;'><div align='center' style='text-align: center;'><font face='Arial' size='4' color='#104160'><b>XXXX Portfolio Confirmation</b></font></div></span></td></tr></tbody></table></td></tr></tbody></table></td></tr></tbody></table></td></tr></tbody></table></td></tr><tr height='8' style='height: 8px;'><td style='padding: 0; border: 0 none black;'><br><table width='600' align='center'style='border-collapse: collapse; width: 600px;'><tbody><tr height='8' style='height: 8px;'><td width='600' style='width: 600px; padding: 0; border: 0 none black;'><img src='cid:space' align='top'alt='space' style='display: inline;'></td></tr></tbody></table></td></tr><tr height='8' style='height: 8px;'><td style='padding: 0; border: 0 none black;'><br><table width='600' align='center'style='border-collapse: collapse; width: 600px;'><tbody><tr height='8' style='height: 8px;'><td width='600'style='width: 600px; padding: 0; border: 0 none black;'><table width='560' align='center'style='border-collapse: collapse; width: 560px;'><tbody><tr height='8' style='height: 8px;'><td width='560'style='width: 560px; padding: 0; border: 0 none black;'><table width='550' align='right'style='border-collapse: collapse; width: 550px;'><tbody><tr height='8' style='height: 8px;'><td width='550'style='width: 550px; padding: 0; border: 0 none black;'></td></tr><tr height='8' style='height: 8px;'><td width='550'style='width: 550px; padding: 0; border: 0 none black;'><table width='550' align='left'style='border-collapse: collapse; width: 550px;'><tbody><tr height='8' style='height: 8px;'><td colspan='2' width='550'style='width: 550px; padding: 0; border: 0 none black;'><font face='Arial' size='4' color='#C21212'><b>Dear Customer</b></font></td></tr><tr height='8' style='height: 8px;'><td colspan='2' width='550'style='width: 550px; padding: 0; border: 0 none black;'><font face='Arial' size='4' color='#104160'><br>Following your instructions we have created your XXXX. The document attached enable you to view the details of your portfolio. You can save, print and store it for your records.</font><font face='Arial' size='4' color='#104160'><br></font><font face='Arial' size='4' color='#104160'><br>For security reasons, the document is protected with a password that has been sent to your mobile by SMS.</font><font face='Arial' size='4' color='#104160'><br></font><font face='Arial' size='4' color='#104160'><br>Many thanks for trusting us to help you manage your finances.</font></td></tr><tr height='8' style='height: 8px;'><td width='510' style='width: 510px; padding: 0; border: 0 none black;'></td><td width='39' style='width: 39px; padding: 0; border: 0 none black;'></td></tr><tr height='8' style='height: 8px;'><td colspan='2' width='550'style='width: 550px; padding: 0; border: 0 noneblack;'><font face='Arial' size='4' color='#C21212'><b>XXXX YYYYY Customer Service</b></font></td></tr><tr height='8' style='height: 8px;'><td width='510' valign='top'style='width: 510px; padding: 0; border: 0 none black;'><table width='510' align='left'style='border-collapse: collapse; width:510px;'><tbody><tr height='8' style='height: 8px;'><td width='510'style='width: 510px; padding:0; border: 0 none black;'></td></tr><tr height='8' style='height: 8px;'><td width='510'style='width: 510px; padding:0; border: 0 none black;'><font face='Times New Roman' size='2'>&nbsp;</font> <div style='margin-top: 0;margin-bottom: 0;'><font face='Times New Roman' size='2'>&nbsp;</font></div><div style='margin-top: 0;margin-bottom: 0;'><font face='Times New Roman' size='2'>&nbsp;</font></div><div style='margin-top: 0;margin-bottom: 0;'><font face='Times New Roman' size='2'>&nbsp;</font></div><div style='margin-top: 0;margin-bottom: 0;'><font face='Arial' size='2'color='#104160'>Please add </font><a href='mailto:XXXXYYYYY@XXXXbank.com.kw'target='_blank'><font face='Arial'size='2'>XXXXYYYYY@XXXXbank.com.kw</font></a><font face='Arial' size='2'color='#104160'> to your email address book to ensure your statements are not redirected to the junk folder.If there are any questions, please contact directly with your XXXX Bank manager.</font></div></td></tr><tr height='8' style='height: 8px;'><td width='510' style='width: 510px; padding: 0; border: 0 none black;'></td></tr></tbody></table></td><td width='39' style='width: 39px; padding: 0; border: 0 none black;'></td></tr></tbody></table> </td></tr></tbody></table></td></tr></tbody></table></td></tr></tbody></table> </td></tr><tr height='8' style='height: 8px;'><td style='padding: 0; border: 0 none black;'><br><table width='600' align='center' style='border-collapse: collapse; width: 600px;'><tbody><tr height='8' style='height: 8px;'><td width='600' style='width: 600px; padding: 0; border-width: 0 0 1px 0; border-style: none none solid none; border-color: black;'></td></tr></tbody></table></td></tr><tr height='8' style='height: 8px;'><td style='padding: 0; border: 0 none black;'><table width='600' align='center' style='border-collapse: collapse; width: 600px;'><tbody><tr height='8' style='height: 8px;'><td width='600' style='width: 600px; padding: 0; border: 0 none black;'></td></tr></tbody></table> <br></td></tr><tr height='8' style='height: 8px;'><td style='background-color: #F0F0F0; padding: 0; border: 0 none black;'><span style='background-color: #F0F0F0;'><table width='600' align='center' style='border-collapse: collapse; width: 600px;'><tbody><tr height='8' style='height: 8px;'><td width='600' style='width: 600px; padding: 0; border-width: 1px 0 0 0; border-style: solid none none none; border-color: #E6E6E6;'><table width='560' align='center'style='border-collapse: collapse; width: 560px;'><tbody><tr height='8' style='height: 8px;'><td width='560' style='width: 560px; padding: 0; border: 0 none black;'></td></tr><tr height='8' style='height: 8px;'><td width='560' style='width: 560px; padding: 0; border: 0 none black;'><table width='500' align='center' style='border-collapse: collapse; width: 500px;'><tbody><tr height='8' style='height: 8px;'><td width='300' style='width: 300px; padding: 0; border: 0 none black;'><div align='center' style='text-align: center;'><font face='Arial' size='2' color='#C21212'>© 2016 XXXX Bank. All rights reserved. </font><font face='Arial' size='2' color='#004080'>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; </font></div></td><td width='30' style='width: 30px; padding: 0; border: 0 none black;'><div align='center' style='text-align: center;'><a href='https://XXX/XXXX?_rdr' target='_blank'><img src='cid:facebook' alt='facebook' style='display:inline;'></a><td width='30' style='width: 30px; padding: 0; border: 0 none black;'><div align='center' style='text-align: center;'><a href='http://XXXX.com/XXXX' target='_blank'><img src='cid:twitter' align='top' alt='twitter' style='display:inline;'></a><td width='30' style='width: 30px; padding: 0; border: 0 none black;'><div align='center' style='text-align: center;'><a href='http://XXXXX.com/XXXX' target='_blank'><img src='cid:Instagram' align='top'alt='Instagram' style='display:inline;'></a><td width='30' style='width: 30px; padding: 0; border: 0 none black;'><div align='center' style='text-align: center;'><a href='https://www.youtube.com/XXX/YYYY' target='_blank'><img src='cid:youtube' align='top' alt='youtube' style='display:inline;'></a><td width='30' style='width: 30px; padding: 0; border: 0 none black;'><div align='center' style='text-align: center;'><a href='https://www.linkedin.com/company/XXXX?trk=hb_tab_compy_id_202412' target='_blank'><img src='cid:linkedIn' align='top' alt='linkedIn' style='display:inline;'></a></div></td></tr></tbody></table></td></tr><tr height='8' style='height: 8px;'><td width='560'style='width: 560px; padding: 0; border: 0 none black;'></td></tr></tbody></table> </td></tr></tbody></table></span></td</tr></tbody></table></div><div style='margin-top: 0; margin-bottom: 0;'><font face='Times New Roman' size='3'>&nbsp;</font></div><div style='margin-top: 0; margin-bottom: 0;'><img src='cid:tree' alt='http://XXXX/tree-small.jpg' style='display:inline;'><font face='Arial' size='1' color='green'>Please consider theenvironment &amp; Print only if necessary</font></div><hr></div></body></html>

我使用字符串准备了邮件内容,并使用CID

嵌入了图像
String htmltext1=mailContentStart.concat(mailContentLogo).concat(mailContentHeading).
                concat(mailContentSpace).concat(mailContentBody);
        String htmltext12=(mailContentImages).concat(mailEnd);  

当我联系htmltext 1和htmltext2时,结果是以下结果

<html><body><div><div style='margin-top: 0; margin-bottom: 0;'><font face='Times New Roman' size='3'>&nbsp;</font></div><div style='margin-top: 0; margin-bottom: 0;'><table width='100%' style='border-collapse: collapse; width: 100%;'><tbody><tr height='8' style='height: 8px;'><td style='width: 100%; padding: 0; border: 0 none black;'><table width='100%'style='border-collapse: collapse; width: 100%;'><tbody><tr height='8' style='height: 8px;'><td style='width: 100%; padding: 0; border: 0 none black;'><table width='600' align='center' style='border-collapse: collapse; width: 600px;><tbody><tr height='8' style='height: 8px;'><td width='600' style='width: 600px; padding: 0; border: 0 none black;'><table width='600' align='center'style='border-collapse: collapse; width: 600px;'><tbody><tr height='8' style='height: 8px;'><td width='600'style='width: 600px; padding: 0; border: 0 none black;'><table width='600' align='left'style='border-collapse: collapse; width: 600px;'><tbody><tr height='8' style='height: 8px;'><td width='600' style='width: 600px; padding: 0;border: 0 none black;'><table width='600' align='left'style='border-collapse:collapse; width: 600px;'><tbody><tr height='8' style='height: 8px;'><td width='600'style='width:600px; padding:0; border: 0 none black;'><div align='center' style='text-align:center;'><a href='http://www.e-XXXXbank.com/'target='_blank'> <img src='cid:logo' 'align='top' alt='logo' style='display:inline;'></a></div></td></tr></tbody></table></td></tr></tbody></table></td></tr></tbody></table></td></tr></tbody></table></td></tr></tbody></table></td></tr><tr height='8' style='height: 8px;'><td style='padding: 0; border: 0 none black;'><br><table width='600' align='center'<tbody><tr height='8' style='height: 8px;'><td width='600'style='width: 600px; padding: 0; border: 0 none black;'><table width='560' align='center'style='border-collapse: collapse; width: 560px;'><tbody><tr height='8' style='height: 8px;'><td width='560'style='width: 560px; padding: 0; border: 0 none black;'><table width='100%'style='border-collapse: collapse; width: 100%;'><tbody><tr height='8' style='height: 8px;'><td style='width: 100%; padding: 0; border: 0 none black;'><table width='560' align='center'style='border-collapse: collapse; width: 560px;'><tbody><tr height='8' style='height: 8px;'><td width='558'style='width: 558px; background-color: #F0F0F0; padding: 0; border: 1px solid #ECECED;'><span style='background-color: #F0F0F0;'><div align='center' style='text-align: center;'><font face='Arial' size='4' color='#104160'><b>XXXX Portfolio Confirmation</b></font></div></span></td></tr></tbody></table></td></tr></tbody></table></td></tr></tbody></table></td></tr></tbody></table></td></tr><tr height='8' style='height: 8px;'><td style='padding: 0; border: 0 none black;'><br><table width='600' align='center'style='border-collapse: collapse; width: 600px;'><tbody><tr height='8' style='height: 8px;'><td width='600' style='width: 600px; padding: 0; border: 0 none black;'><img src='cid:space' align='top'alt='space' style='display: inline;'></td></tr></tbody></table></td></tr><tr height='8' style='height: 8px;'><td style='padding: 0; border: 0 none black;'><br><table width='600' align='center'style='border-collapse: collapse; width: 600px;'><tbody><tr height='8' style='height: 8px;'><td width='600'style='width: 600px; padding: 0; border: 0 none black;'><table width='560' align='center'style='border-collapse: collapse; width: 560px;'><tbody><tr height='8' style='height: 8px;'><td width='560'style='width: 560px; padding: 0; border: 0 none black;'><table width='550' align='right'style='border-collapse: collapse; width: 550px;'><tbody><tr height='8' style='height: 8px;'><td width='550'style='width: 550px; padding: 0; border: 0 none black;'></td></tr><tr height='8' style='height: 8px;'><td width='550'style='width: 550px; padding: 0; border: 0 none black;'><table width='550' align='left'style='border-collapse: collapse; width: 550px;'><tbody><tr height='8' style='height: 8px;'><td colspan='2' width='550'style='width: 550px; padding: 0; border: 0 none black;'><font face='Arial' size='4' color='#C21212'><b>Dear Customer</b></font></td></tr><tr height='8' style='height: 8px;'><td colspan='2' width='550'style='width: 550px; padding: 0; border: 0 none black;'><font face='Arial' size='4' color='#104160'><br>Following your instructions we have created your XXXX. The document attached enable you to view the details of your portfolio. You can save, print and store it for your records.</font><font face='Arial' size='4' color='#104160'><br></font><font face='Arial' size='4' color='#104160'><br>For security reasons, the document is protected with a password that has been sent to your mobile by SMS.</font><font face='Arial' size='4' color='#104160'><br></font><font face='Arial' size='4' color='#104160'><br>Many thanks for trusting us to help you manage your finances.</font></td></tr><tr height='8' style='height: 8px;'><td width='510' style='width: 510px; padding: 0; border: 0 none black;'></td><td width='39' style='width: 39px; padding: 0; border: 0 none black;'></td></tr><tr height='8' style='height: 8px;'><td colspan='2' width='550'style='width: 550px; padding: 0; border: 0 noneblack;'><font face='Arial' size='4' color='#C21212'><b>XXXX YYYYY Customer Service</b></font></td></tr><tr height='8' style='height: 8px;'><td width='510' valign='top'style='width: 510px; padding: 0; border: 0 none black;'><table width='510' align='left'style='border-collapse: collapse; width:510px;'><tbody><tr height='8' style='height: 8px;'><td width='510'style='width: 510px; padding:0; border: 0 none black;'></td></tr><tr height='8' style='height: 8px;'><td width='510'style='width: 510px; padding:0; border: 0 none black;'><font face='Times New Roman' size='2'>&nbsp;</font> <div style='margin-top: 0;margin-bottom: 0;'><font face='Times New Roman' size='2'>&nbsp;</font></div><div style='margin-top: 0;margin-bottom: 0;'><font face='Times New Roman' size='2'>&nbsp;</font></div><div style='margin-top: 0;margin-bottom: 0;'><font face='Times New Roman' size='2'>&nbsp;</font></div><div style='margin-top: 0;margin-bottom: 0;'><font face='Arial' size='2'color='#104160'>Please add </font><a href='mailto:XXXXYYYYY@XXXXbank.com.kw'target='_blank'><font face='Arial'size='2'>XXXXYYYYY@XXXXbank.com.kw</font></a><font face='Arial' size='2'color='#104160'> to your email address book to ensure your statements are not redirected to the junk folder.If there are any questions, please contact directly with your XXXX Bank manager.</font></div></td></tr><tr height='8' style='height: 8px;'><td width='510' style='width: 510px; padding: 0; border: 0 none black;'></td></tr></tbody></table></td><td width='39' style='width: 39px; padding: 0; border: 0 none black;'></td></tr></tbody></table> </td></tr></tbody></table></td></tr></tbody></table></td></tr></tbody></table> </td></tr><tr height='8' style='height: 8px;'><td style='padding: 0; border: 0 none black;'><br><table width='600' align='center' style='border-collapse: collapse; width: 600px;'><tbody><tr height='8' style='height: 8px;'><td width='600' style='width: 600px; padding: 0; border-width: 0 0 1px 0; border-style: none none solid none; border-color: black;'></td></tr></tbody></table></td></tr><tr height='8' style='height: 8px;'><td style='padding: 0; border: 0 none black;'><table width='600' align='center' style='border-collapse: collapse; width: 600px;'><tbody><tr height='8' style='height: 8px;'><td width='600' style='width: 600px; padding: 0; border: 0 none black;'></td></tr></tbody></table> <br></td></tr><tr height='8' style='height: 8px;'><td style='background-color: #F0F0F0; padding: 0; border: 0 none black;'><span style='background-color: #F0F0F0;'><table width='600' align='center' style='border-collapse: collapse; width: 600px;'><tbody><tr height='8' style='height: 8px;'><td width='600' style='width: 600px; padding: 0; border-width: 1px 0 0 0; border-style: solid none none none; border-color: #E6E6E6;'><table width='560' align='center'style='border-collapse: collapse; width: 560px;'><tbody><tr height='8' style='height: 8px;'><td width='560' style='width: 560px; padding: 0; border: 0 none black;'></td></tr><tr height='8' style='height: 8px;'><td width='560' style='width: 560px; padding: 0; border: 0 none black;'><table width='500' align='center' style='border-collapse: collapse; width: 500px;'><tbody><tr height='8' style='height: 8px;'><td width='300' style='width: 300px; padding: 0; border: 0 none black;'><div align='center' style='text-align: center;'><font face='Arial' size='2' color='#C21212'>© 2016 XXXX Bank. All rights reserved. </font><font face='Arial' size='2' color='#004080'>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; </font></div></td><td width='30' style='width: 30px; padding: 0; border: 0 none black;'><div align='center' style='text-align: center;'><a href='https://XXX/XXXX?_rdr' target='_blank'><img src='cid:facebook' alt='facebook' style='display:inline;'></a><td width='30' style='width: 30px; padding: 0; border: 0 none black;'><div align='center' style='text-align: center;'><a href='http://XXXX.com/XXXX' target='_blank'><img src='cid:twitter' align='top' alt='twitter' style='display:inline;'></a><td width='30' style='width: 30px; padding: 0; border: 0 none black;'><div align='center' style='text-align: center;'><a href='http://XXXXX.com/XXXX' target='_blank'><img src='cid:Instagram' align='top'alt='Instagram' style='display:inline;'></a><td width='30' style='width: 30px; padding: 0; border: 0 none black;'><div align='center' style='text-align: center;'><a href='https://www.youtube.com/XXX/YYYY' target='_blank'><img src='cid:youtube' align='top' alt='youtube' style='display:inline;'></a><td width='30' style='width: 30px; padding: 0; border: 0 none black;'><div align='center' style='text-align: center;'><a href='htt.....

任何人都可以建议解决相同的问题。因此,我的HTML无法在MAIL中正确呈现。

3 个答案:

答案 0 :(得分:0)

我建议你在这里使用StringBuilder而不是concat。 或者如果不是这样,那么在这里简单地使用+运算符而不是concat。

concat()方法只接受String值,而+运算符将静默地将参数转换为String(对象使用toString()方法)。所以concat()方法在它接受的内容中更为严格。

要使用 StringBuilder ,您可以使用以下代码替换代码中的上述两行:

StringBuilder html = new StringBuilder();
html.append(mailContentStart)
        .append(mailContentLogo)
        .append(mailContentHeading)
        .append(mailContentSpace)
        .append(mailContentBody)
        .append(mailContentImages)
        .append(mailEnd); 

您可以通过执行以下操作返回此StringBuilder的String值:

return html.toString();

答案 1 :(得分:0)

使用StringBuilder并在创建电子邮件时附加内容,如果funstion使用String,只需使用toString()传递StringBuilder

答案 2 :(得分:0)

你的html中缺少':

<table width='600' align='center' style='border-collapse: collapse; width: 600px;>