美文网首页
2018-12-19

2018-12-19

作者: 不需要什么就能清醒 | 来源:发表于2018-12-19 16:53 被阅读0次

EWS

按照cron表达式自动执行寻找地址存放某个时间段的文件

package com.smile.schedule;

import java.util.HashSet;

import java.util.Set;

import org.springframework.scheduling.annotation.EnableScheduling;

import org.springframework.scheduling.annotation.Scheduled;

import org.springframework.stereotype.Component;

import com.smile.ews.EwsMailAttService;

import com.smile.ews.EwsMailVO;

@Component

@EnableScheduling

public class ScheduledTask {

    // 两种表达式:Seconds Minutes Hours DayofMonth Month DayofWeek Year或

    // Seconds Minutes Hours DayofMonth Month DayofWeek

    // 每隔5秒执行一次:*/5 * * * * ?

    // 每隔1分钟执行一次:0 */1 * * * ?

    // 每天23点执行一次:0 0 23 * * ?

    // 每天凌晨1点执行一次:0 0 1 * * ?

    // 每月1号凌晨1点执行一次:0 0 1 1 * ?

    // 每天0点运行一次 0 0 0 * * ?

// 每天晚上七点执行 "0 0 19 * * ?"

    // 每一天凌晨1点执行一次

//    @Scheduled(cron = "0 */1 * * * ?")

    @Scheduled(cron = "0 0 19 * * ?")

    public void reportCurrentByCron() {

        EwsMailAttService service = new EwsMailAttService();

        EwsMailVO ewsMail = new EwsMailVO();

        ewsMail.setUrl("https://autodiscover.boshi.com.cn/ews/Exchange.asmx");

        ewsMail.setUsername("CFETS");

        ewsMail.setPassword("123QWEasd");

        Set<String> targetFiles = new HashSet<>();

        //文件存放的地址\\\\ia\\temp\\liuxt\\1

        targetFiles.add("\\\\ia\\temp\\liuxt\\1");

        // targetFiles.add("\\\\ia\\temp\\liuxt\\1\\");

        // targetFiles.add("\\\\ia\\temp\\liuxt\\1\\2\\3");

        // targetFiles.add("\\\\ia\\temp\\liuxt\\2\\3");

        ewsMail.setTargetFiles(targetFiles);

        service.initMethod(ewsMail);

        try {

service.saveAtt(ewsMail);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

    }

}

EwsEmail格式

package com.smile.ews;

import java.util.Set;

import lombok.Data;

@Data

public class EwsMailVO {

    /**

    * 邮件服务器地址

    */

    private String url;

    /**

    * 邮箱账户

    */

    private String username;

    /**

    * 邮箱密码

    */

    private String password;

    /**

    * 目标文件

    */

    private Set<String> targetFiles;

public String getUrl() {

return url;

}

public void setUrl(String url) {

this.url = url;

}

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public Set<String> getTargetFiles() {

return targetFiles;

}

public void setTargetFiles(Set<String> targetFiles) {

this.targetFiles = targetFiles;

}

}

主要过程

package com.smile.ews;

import java.io.ByteArrayInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.net.URI;

import java.net.URISyntaxException;

import java.nio.file.Files;

import java.nio.file.Path;

import java.nio.file.Paths;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.HashSet;

import java.util.Set;

import javax.annotation.PostConstruct;

import org.apache.commons.lang3.time.DateUtils;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.springframework.util.CollectionUtils;

import microsoft.exchange.webservices.data.core.ExchangeService;

import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;

import microsoft.exchange.webservices.data.core.enumeration.property.WellKnownFolderName;

import microsoft.exchange.webservices.data.core.enumeration.search.LogicalOperator;

import microsoft.exchange.webservices.data.core.service.item.EmailMessage;

import microsoft.exchange.webservices.data.core.service.item.Item;

import microsoft.exchange.webservices.data.core.service.schema.EmailMessageSchema;

import microsoft.exchange.webservices.data.credential.ExchangeCredentials;

import microsoft.exchange.webservices.data.credential.WebCredentials;

import microsoft.exchange.webservices.data.property.complex.Attachment;

import microsoft.exchange.webservices.data.property.complex.AttachmentCollection;

import microsoft.exchange.webservices.data.property.complex.FileAttachment;

import microsoft.exchange.webservices.data.search.FindItemsResults;

import microsoft.exchange.webservices.data.search.ItemView;

import microsoft.exchange.webservices.data.search.filter.SearchFilter;

public class EwsMailAttService {

    private ExchangeService service = null;

    @PostConstruct

    public void initMethod(EwsMailVO ewsMail) {

        service = new ExchangeService(ExchangeVersion.Exchange2010);

        ExchangeCredentials credentials = new WebCredentials("CFETS", "123QWEasd");

        service.setCredentials(credentials);

        try {

            service.setUrl(new URI("https://autodiscover.boshi.com.cn/ews/Exchange.asmx"));

        } catch (URISyntaxException e) {

            e.printStackTrace();

        }

    }

    public void saveAtt(EwsMailVO ewsMail) throws Exception {

        Set<String> targetfiles = ewsMail.getTargetFiles();

        if (CollectionUtils.isEmpty(targetfiles)) {

            return;

        }

        // 质押式回购市场交易情况总结日报_20180816

        ItemView view = new ItemView(Integer.MAX_VALUE);

        Date currentTime = new Date();

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");//日期格式

        String startTime = formatter.format(currentTime)+" 00:00:00";//当前最小日期

        String endTime = formatter.format(currentTime)+" 23:59:59";//当前最大日期

        SearchFilter start = new SearchFilter.IsGreaterThanOrEqualTo(EmailMessageSchema.DateTimeCreated, DateUtils.parseDate(startTime, "yyyy-MM-dd HH:mm:ss"));

        SearchFilter end = new SearchFilter.IsLessThanOrEqualTo(EmailMessageSchema.DateTimeCreated, DateUtils.parseDate(endTime, "yyyy-MM-dd HH:mm:ss"));

        SearchFilter filter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, start, end);

        FindItemsResults<Item> findResults = service.findItems(WellKnownFolderName.Inbox, filter, view);

        ArrayList<Item> items = findResults.getItems();

        Set<String> xls = new HashSet<>();

        for (Item item : items) {

            EmailMessage message = EmailMessage.bind(service, item.getId());

            if (message.getHasAttachments()) {

                // TODO 如果没有附件会否报错

                AttachmentCollection atts = message.getAttachments();

                for (Attachment attachment : atts) {

                    // 附件可能是邮件,也可能是文件。邮件的情况应该递归,但是业务上暂时遇不到,所以这里只处理文件类型的附件

                    if (attachment instanceof FileAttachment) {

                        FileAttachment att = (FileAttachment) attachment;

                        // 复制到指定的多个目录

                        for (String targetfile : targetfiles) {

                            Path path = Paths.get(targetfile);

                            // 层层创建目录

                            Files.createDirectories(path);

                            // 将文件名追加到路径

                            path = path.resolve(att.getName());

                            att.load();

                            // 需要判断是否xls

                            InputStream is = new ByteArrayInputStream(att.getContent());

                            HSSFWorkbook workbook = new HSSFWorkbook(is);

                            FileOutputStream os = new FileOutputStream(path.toFile());

                            workbook.write(os);

                            os.close();

                            workbook.close();

                        }

                    }

                }

            }

        }

        service.close();

    }

}

springboot启动

package com.smile;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class CopyEmailApplication {

    public static void main(String[] args) {

        SpringApplication.run(CopyEmailApplication.class, args);

    }

}

POM配置文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>ewas</groupId>

  <artifactId>email</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <name>email</name>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.5.6.RELEASE</version>

</parent>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<java.version>1.7</java.version>

<springboot.mybatis.version>1.3.0</springboot.mybatis.version>

<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>

<thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>

<quartz.version>2.2.3</quartz.version>

<commons-lang3.version>3.6</commons-lang3.version>

<commons-io.version>2.5</commons-io.version>

<commons-exec.version>1.3</commons-exec.version>

<fastjson.version>1.2.37</fastjson.version>

</properties>

<dependencies>

<!-- Spring -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-tx</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context-support</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

<!-- Commons -->

<dependency>

<groupId>org.apache.commons</groupId>

<artifactId>commons-lang3</artifactId>

<version>${commons-lang3.version}</version>

</dependency>

<dependency>

<groupId>commons-io</groupId>

<artifactId>commons-io</artifactId>

<version>${commons-io.version}</version>

</dependency>

<dependency>

<groupId>org.apache.commons</groupId>

<artifactId>commons-exec</artifactId>

<version>${commons-exec.version}</version>

</dependency>

<!-- Quartz -->

<dependency>

<groupId>org.quartz-scheduler</groupId>

<artifactId>quartz</artifactId>

<version>${quartz.version}</version>

</dependency>

<dependency>

<groupId>org.quartz-scheduler</groupId>

<artifactId>quartz-jobs</artifactId>

<version>${quartz.version}</version>

</dependency>

<!-- Json -->

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>fastjson</artifactId>

<version>${fastjson.version}</version>

</dependency>

<dependency>

<groupId>com.microsoft.sqlserver</groupId>

<artifactId>sqljdbc4</artifactId>

<version>4.0</version>

</dependency>

<!-- EWS -->

<!-- <dependency> -->

<!-- <groupId>com.microsoft.ews-java-api</groupId> -->

<!-- <artifactId>ews-java-api</artifactId> -->

<!-- <version>1.1.1</version> -->

<!-- </dependency> -->

<!-- <dependency> -->

<!-- <groupId>commons-httpclient</groupId> -->

<!-- <artifactId>commons-httpclient</artifactId> -->

<!-- <version>3.1</version> -->

<!-- </dependency> -->

<!-- Lombox -->

<dependency>

<groupId>org.projectlombok</groupId>

<artifactId>lombok</artifactId>

<version>1.18.2</version>

<scope>provided</scope>

</dependency>

<!-- Ews -->

<dependency>

<groupId>com.microsoft.ews-java-api</groupId>

<artifactId>ews-java-api</artifactId>

<version>2.0</version>

</dependency>

<!-- poi -->

        <dependency>

            <groupId>org.apache.poi</groupId>

            <artifactId>poi</artifactId>

            <version>3.16</version>

        </dependency>

</dependencies>

<build>

<finalName>email</finalName>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

</plugins>

</build>

</project>

相关文章

网友评论

      本文标题:2018-12-19

      本文链接:https://www.haomeiwen.com/subject/ksgjkqtx.html