您的位置:首页 > 其它

csv 读取 邮件附件读取

2016-04-28 18:10 363 查看
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
* @return	filePath		    附件地址
* @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;

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: