csv 读取 邮件附件读取

来源:互联网 发布:淘宝怎么赚钱兼职工作 编辑:程序博客网 时间:2024/05/05 11:43

csv 读取 

package com.wanwan.csv;import java.io.FileNotFoundException;import java.io.IOException;import java.nio.charset.Charset;import com.csvreader.CsvReader;public class ExcelReader {private static String path = "F:/os/受访域名_20160427-20160427.csv";    public static void main(String[] args){      //生成CsvReader对象,以,为分隔符,GBK编码方式        CsvReader r;try {r = new CsvReader(path, ',',Charset.forName("GBK")); //读取表头       r.readHeaders();       //逐条读取记录,直至读完       while (r.readRecord()) {           System.out.println(r.get("域名").trim()+"\t"+r.get("浏览量(PV)").trim()+"\t"+r.get("访客数(UV)").trim()+"\t"+r.get("IP数").trim()+"\t"+r.get("跳出率").trim()+"\t"+r.get("平均停留时长").trim());       }       r.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}           }  }





 

pom<dependency><groupId>net.sourceforge.javacsv</groupId><artifactId>javacsv</artifactId><version>2.0</version></dependency>




读取email附件

import java.io.DataInputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.util.Properties;import javax.mail.Address;import javax.mail.BodyPart;import javax.mail.Folder;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.Multipart;import javax.mail.NoSuchProviderException;import javax.mail.Session;import javax.mail.Store;import javax.mail.internet.MimeUtility; public class getMailMsg {public static void main(String[] args) throws IOException {getMail();}public static void getMail() throws IOException {String host = "pop.163.com";String userName = "javahadoop@163.com";String passWord = "************";Properties props = new Properties();Session session = Session.getDefaultInstance(props);session.setDebug(false);try {System.out.println("receive...............................");Store store = session.getStore("pop3");store.connect(host, userName, passWord);// 验证Folder folder = store.getFolder("INBOX");// 取得收件文件夹folder.open(Folder.READ_WRITE);Message msg[] = folder.getMessages();System.out.println("邮件个数:" + msg.length);  for (int i = 0; i < msg.length; i++) {Message message = msg[i];Address address[] = message.getFrom();StringBuffer from = new StringBuffer();// 此for循环是我项目测试用的for (int j = 0; j < address.length; j++) {if (j > 0) from.append(";");from.append(address[j].toString());}String emailfrom = from.toString();if(emailfrom.indexOf("qiaoyehui@yappam.com") > 0){ System.out.println(message.getMessageNumber());System.out.println("来自:" + from.toString());System.out.println("大小:" + message.getSize());System.out.println("主题:" + message.getSubject());System.out.println("时间::" + message.getSentDate()); // 得到邮件的Multipart(内容总部件--【包涵附件】)        Multipart multipart = (Multipart) message.getContent();               int count = multipart.getCount();    // 部件个数                for(int x =0; x<count; x++) {         BodyPart part = multipart.getBodyPart(x);  // 单个部件     注意:单个部件有可能又为一个Multipart,层层嵌套         // 单个部件类型             String type = part.getContentType().split(";")[0];             if(type.contains("application/")) {        // 应用附件 (zip、xls、docx等)            //下载附件             DataInputStream in = new DataInputStream(part.getInputStream());  // 打开附件的输入流             FileOutputStream out=null;             // 获取附件名              String fileName = part.getFileName();                // 文件名解码                  fileName = MimeUtility.decodeText(fileName);                  System.out.println("文件名解码后的名字:"+fileName);                  File file = new File("F:/os/" + fileName); // 查看是否有当前文件                  if(!file.exists()){                  out = new FileOutputStream(file);                  int data;                  while((data=in.read()) != -1) {                          out.write(data);                      }                      System.out.println("附件:【" + fileName + "】下载完毕,保存路径为:" + file.getPath());                  }                  // 关流                  if(in != null) {                      in.close();                  }                  if(out != null) {                      out.close();                  }             }        }System.out .println("===================================================");}} folder.close(true);// 设置关闭store.close();System.out.println("receive over............................");} catch (NoSuchProviderException e) {e.printStackTrace();} catch (MessagingException e) {e.printStackTrace();}}}



import java.io.BufferedOutputStream;import java.io.DataInputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.nio.charset.Charset;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.Enumeration;import java.util.List;import java.util.Properties;import javax.mail.Address;import javax.mail.BodyPart;import javax.mail.Folder;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.Multipart;import javax.mail.Session;import javax.mail.Store;import javax.mail.internet.MimeUtility;import com.csvreader.CsvReader;  public class EmailGetAttachments {private final String HOST = "pop.163.com";//服务器private final String USERNAME ="javahadoop@163.com";//用户名private final String PASSWORD = "*****************";//用户密码   一般为授权码private final String CONFIG_PATH = "F:/os/";//文件保存路径private final String WEBSITES = "F:/os/";private final String MAINFROM ="autopost@baidu.com";/** * 得到邮箱附件 * @param emailFromAddress   发件人                例如:autopost@baidu.com * @param emailDates    发件人发件日期       例如:2016-04-05 * @returnfilePath    附件地址 * @throws MessagingException  * @throws IOException  */public List<String> getZipAttachments(String emailFromAddress ,List<String> emailDates) throws MessagingException, IOException{List<String> filePath =new  ArrayList<String>();//解压后文件的路径Properties props = new Properties();Session session = Session.getDefaultInstance(props);session.setDebug(false);Store store = session.getStore("pop3");store.connect(HOST, USERNAME, PASSWORD);// 验证Folder folder = store.getFolder("INBOX");// 取得收件文件夹folder.open(Folder.READ_WRITE);Message msg[] = folder.getMessages(); for (Message message : msg) {Address address[] = message.getFrom();StringBuffer from = new StringBuffer();for (int j = 0; j < address.length; j++) {if (j > 0)from.append(";");from.append(address[j].toString());}String mailComeaddress = from.toString();//判断发件人if(mailComeaddress.indexOf(emailFromAddress) >= 0){for (String emailDatetime :emailDates) {if(dateToString(message.getSentDate(),"yyyy-MM-dd").equals(emailDatetime)){Multipart multipart = (Multipart) message.getContent();int count = multipart.getCount(); // 部件个数for (int x = 0; x < count; x++) {BodyPart part = multipart.getBodyPart(x); // 单个部件String type = part.getContentType().split(";")[0]; // 单个部件类型if (type.contains("application/")) {DataInputStream in = new DataInputStream(part.getInputStream()); // 打开附件的输入流FileOutputStream out = null;String fileName = part.getFileName(); // 获取附件名fileName = MimeUtility.decodeText(fileName); // 文件名解码String path = CONFIG_PATH + "baidutongji/";File file = new File(path + fileName); // 查看是否有当前文件有删掉if(file.exists())file.delete();out = new FileOutputStream(file);int data;while ((data = in.read()) != -1){out.write(data);}// 关流if (in != null)in.close();if (out != null)out.close();filePath.add(path+fileName);}}}} }}return filePath;}/** *  解压文件    是否删除原有压缩文件 * @param filePath 压缩文件地址 * @param bool        是否删除原有文件压缩文件 * @return */public  boolean unZip(String filePath,boolean bool) {File zipFile = new File(filePath);if(zipFile.exists()){org.apache.tools.zip.ZipFile zip = null;String unZipPath = zipFile.getParent();try {zip = new org.apache.tools.zip.ZipFile(zipFile, "GBK");Enumeration<org.apache.tools.zip.ZipEntry> en = zip.getEntries();org.apache.tools.zip.ZipEntry entry = null;byte[] buffer = new byte[8192];int length = -1;InputStream input = null;BufferedOutputStream bos = null;File file = null;while (en.hasMoreElements()) {entry = (org.apache.tools.zip.ZipEntry) en.nextElement();if (entry.isDirectory()) {continue;}input = zip.getInputStream(entry);file = new File(unZipPath, entry.getName());if (!file.getParentFile().exists()) {file.getParentFile().mkdirs();}bos = new BufferedOutputStream(new FileOutputStream(file));while (true) {length = input.read(buffer);if (length == -1)break;bos.write(buffer, 0, length);}bos.close();input.close();if(bool){zipFile.delete();}}} catch (IOException e) {e.printStackTrace();}}return false;}/** * 日期格式化 * @param date * @param format * @return */private String dateToString(Date date,String format){if(date == null){return null;}SimpleDateFormat dateFormater = new SimpleDateFormat(format);return dateFormater.format(date) ;}/** * csv文件 * @param csvpath 文件路径 * @return */private String getDateFromCsv(String csvpath){File file = new File(csvpath);if(file.exists()){CsvReader r;try {r = new CsvReader(csvpath, ',', Charset.forName("GBK"));while (r.readRecord()) {//r.get(0)//r.get("域名")}} catch (IOException e) {e.printStackTrace();}  return csvpath;}else{return null;}}}



0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 12306证件号码已被注册怎么办 12306忘记手机号和邮箱怎么办 发邮件被对方服务器退回怎么办 铁路12306显示已注册怎么办 qq密码太长输不进去怎么办 淘宝买家收货地址填写不全怎么办 护士电子注册账户未激活怎么办 您的邮件被退回怎么办 给国外发信被退怎么办 苹果8icloud满了怎么办 吃人参回奶了怎么办 邮箱被黑客黑了怎么办 传图识字有表格怎么办 手机qq收件箱图片打不开怎么办 腾讯企业邮箱一直被攻击怎么办 qq邮箱发送文件太大怎么办 苹果手机邮箱被删除了怎么办 亚马逊卖家登录邮箱被盗怎么办 邮箱名字被注册了怎么办 忘了注册的邮箱名字怎么办 大众车钥匙丢了怎么办 锁柜钥匙丢了怎么办 邮箱的储存空间太小怎么办 扣扣邮箱不支持打开文件怎么办 邮箱大师群发不了邮件怎么办 邮政uk密码忘了怎么办 dns配置错误网页打不开怎么办 手机邮箱收不到邮件怎么办 wifi的那个用户密码错了怎么办 天翼校园客户端连不上怎么办 公司不小心外发邮件怎么办 steam账号被盗邮箱被改怎么办 steam被盗绑定邮箱被改怎么办 简历邮件发错了怎么办 发了简历不回复怎么办 发了简历没回复怎么办 发了简历邮件没有回复怎么办 快手绑定的手机号不用了怎么办 绑定银行卡的手机号不用了怎么办 oppo账号密码忘了怎么办 魅族账号密码忘了怎么办