java发送邮件将附件变成压缩包_浅谈JavaMail的应用之[文件下载打包并发送邮件的实现]...

throws ServletException, IOException {

List list = new ArrayList();

String[] path1 = new String[2];

//从硬盘获取要压缩的文件路径

path1[0] = "F:/测试/in1.xml";

path1[1] = "F:/测试/out1.xml";

list.add(path1);

String[] path2 = new String[2];

path2[0] = "F:/测试/in2.xml";

path2[1] = "F:/测试/out2.xml";

list.add(path2);

//================================================================================================

String gpIdAndNoStr = request.getParameter("gpIdAndNo");

String chosedReason = request.getParameter("chosedReason");

System.out.println("选择"+chosedReason+"模块");

//要生成压缩包的存储地址和文件名

//String desPath = "/本地磁盘路径/压缩文件名.zip";

//定义下载文件头

//response.setHeader("Content-Disposition", "attachment"+";filename="+gpIdAndNoStr+".zip");

//File zipFile = new File(desPath);

//获取上下文(当前项目的具有唯一性)

//String contextPath = request.getSession().getServletContext().getRealPath("/");

//返回此抽象路径的绝对路径的字符串

//File filepath = new File("");

//String contextPath = filepath.getAbsolutePath();

String ph = Thread.currentThread().getContextClassLoader().getResource("").getPath();

int num = ph.indexOf(".metadata");

String contextPath = ph.substring(1, num).replace('/', '\\')+request.getContextPath();

System.out.println("路径:"+contextPath);

//创建一个存放临时文件的目录

File tempFiles = new File(contextPath,"temp");

if(!tempFiles.exists()){

tempFiles.mkdirs();

}

//要生成压缩包的存储地址和文件名

//File tempFile = File.createTempFile(gpIdAndNoStr+"_", ".zip");

File tempFile = new File(tempFiles.getCanonicalPath()+"/"+gpIdAndNoStr+".zip");

if(!tempFile.exists()){

tempFile.createNewFile();

}

ZipOutputStream zipStream = null;

//创建输入流对象

FileInputStream zipSource = null;

//定义缓冲流

BufferedInputStream bufferStream = null;

//压缩条目不是具体的文件,而是压缩文件列表中的列表项。称为条目,就像索引一样

ZipEntry zipEntry = null;

//定义文件和目录路径的抽象表示形式

File file = null;

SimpleDateFormat sdf = new SimpleDateFormat();

//获取当前系统日期

Date date = new Date();

String dateStr = sdf.format(date);

int k =1;

try{

//构造最终压缩包的输出流

zipStream = new ZipOutputStream(new FileOutputStream(tempFile));

System.out.println("创建:"+tempFile.getAbsolutePath());

//zipStream = new ZipOutputStream(responce.getOutputStream());

for(int i=0; i

//通过List中获取的服务器文件下载路径,得到文件和目录路径名称的抽象表示形式的对象

file = new File(list.get(i)[0]);

//将要压缩的文件格式化为输入流

zipSource = new FileInputStream(file);

//定义压缩包中输入文件的文件名及目录

zipEntry = new ZipEntry(gpIdAndNoStr+"_"+dateStr+"_in_"+k+".xml");

//定义该压缩条目位置,开始写入文件到压缩包

zipStream.putNextEntry(zipEntry);

//输入缓冲流

bufferStream = new BufferedInputStream(zipSource,1024*4);

int len = 0;

//创建读写缓冲区

byte[] buf = new byte[1024*4];

//读入字节流

while((len = bufferStream.read(buf))!=-1){

zipStream.write(buf, 0, len);

}

//通过List中获取的服务器文件下载路径,得到文件和目录路径名称的抽象表示形式的对象

file = new File(list.get(i)[1]);

//将要压缩的文件格式化为输入流

zipSource = new FileInputStream(file);

//定义压缩包中输入文件的文件名及目录

zipEntry = new ZipEntry(gpIdAndNoStr+"_"+dateStr+"_out_"+k+".xml");

//定义该压缩条目位置,开始写入文件到压缩包

zipStream.putNextEntry(zipEntry);

//输入缓冲流

bufferStream = new BufferedInputStream(zipSource,1024*4);

//读入字节流

while((len = bufferStream.read(buf))!=-1){

zipStream.write(buf, 0, len);

}

k++;

}

//关闭流

zipStream.close();

bufferStream.close();

//此处调用邮件发送功能

SenderMail.createAttachMail(tempFile.getCanonicalPath(), gpIdAndNoStr);

//通过压缩包文件存储路径,得到文件和目录路径名的抽象表示形式的对象

File tpfile = new File(tempFile.getCanonicalPath());

//判断目录文件是否存在

if(tpfile.exists()){

//如果存在,则删除目录下的文件

tpfile.delete();

}

}catch(Exception e){

e.printStackTrace();

}finally {

try{

if(null != zipSource)

zipSource.close();

}catch(IOException e1){

e1.printStackTrace();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值