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
- csv 读取 邮件附件读取
- 读取CSV
- 读取CSV
- 读取csv
- 如何读取csv文件
- CSV文件直接读取
- 读取CSV到DataTable
- 读取CSV到DataGrid
- C#读取CSV文件
- JAVA读取CSV
- java读取CSV文件
- ajax 读取 .csv 文件
- Java读取CSV文件
- 读取CSV格式文件
- Java 读取 CSV 文件
- 读取.csv文件数据
- 实用读取CSV
- csv文件读取
- 基于jsoup爬虫下载图库
- HttpClient与httpComponents、HttpPost、HttpGet
- iOS字典创建后,没有key。。。
- C++库文件解析(conio.h)
- Integer.valueOf(int i)与自动拆箱与装箱
- csv 读取 邮件附件读取
- Oracle单行函数
- Android获取随机三位数
- 【python新手】---基础算法(1)
- spark streaming 批量索引数据到 elasticsearch
- R语言基本知识梳理(1)-基本管理及向量操作
- Android酷炫实用的开源框架(UI框架)
- ubuntu中通过U盘制作启动盘
- MySQL索引原理及慢查询优化