maven项目构建步步深入《二》
?
public byte[] generateCaptchaImage(String captchaKey) throws Exception {private DefaultKaptcha productor;BufferedImage image = productor.createImage(captchaKey);ByteArrayOutputStream out = new ByteArrayOutputStream();try {ImageIO.write(image, "jpg", out);} catch (Exception e) {e.printStackTrace();}return out.toByteArray();}
?其实上一篇里面也有些基础类,这里整理一下(发送邮件和解析xml写入xml):
2.使用spring3.x注解来注入发送邮件:
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><context:property-placeholder location="classpath:service.properties" /><bean id="javaMailSender" p:host="${email.host}" p:port="${email.port}"p:username="${email.username}" p:password="${email.password}"><property name="javaMailProperties"><props><prop key="mail.${email.protocol}.auth">${email.auth}</prop></props></property></bean>....
?具体发送邮件实现 java:
MimeMessage mimeMessage=javaMailSender.createMimeMessage(); MimeMessageHelper messageHelper=new MimeMessageHelper(mimeMessage); messageHelper.setFrom(systemEmail); messageHelper.setTo(to); messageHelper.setSubject(subject); messageHelper.setText(htmlText,true); javaMailSender.send(mimeMessage);
?3.根据文件名读取xml:
private Document readDocument(){ File dataFile = new File(file); if(!dataFile.exists()){ dataFile.getParentFile().mkdirs(); Document doc=DocumentFactory.getInstance().createDocument(); Element rootElement = doc.addElement(ELEMENT_ROOT); rootElement.addElement(ELEMENT_ACCOUNTS); writeDocument(doc); } try { return reader.read(new File(file));。。。private void writeDocument(Document doc) { OutputStreamWriter out = null; try { out = new OutputStreamWriter(new FileOutputStream(file)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } XMLWriter writer = new XMLWriter(out, OutputFormat.createPrettyPrint()); try { writer.write(doc); } catch (IOException e) { e.printStackTrace(); }finally{ try {out.close();
?
4、读取xml并解析成对象:
public Account readAccount(String id) { Document document = readDocument(); Element element = document.getRootElement().element(ELEMENT_ACCOUNTS); for (Element ele : ((List<Element>)element.elements())) { if(ele.elementText(ACCOUNT_ID).equals(id)){ return buildAccount(ele); } } return null; } private Account buildAccount(Element ele) { Account account=new Account(); account.setId(ele.elementText(ACCOUNT_ID)); account.setName(ele.elementText(ACCOUNT_NAME));。。。?聚合场景:当我们有多个模块,我们会想要一次构建多个项目,而不是分别到两个模块目录下分别执行mvn命令。实现:创建一个额外的名为account-aggregator模块,然后听过该模块构建整个项目的所有模块。aggregator作为一个maven项目,特殊的地方为<packaging>pom</packaging><artifactId>account-aggregator</artifactId> .... <packaging>pom</packaging> <modules> <module>../account-email</module> <module>../account-persist</module>..1.?对于聚合模块,其打包方式packaging的值必须为pom,否则就无法创建。
<parent><groupId>com.ycq.account</groupId><artifactId>account-parent</artifactId><version>0.0.1-SNAPSHOT</version><relativePath>../account-aggregator/account-parent/pom.xml</relativePath></parent>??