从附件名连接到这个servlet!
DownloadServlet.java
package MailServlet;
http://www.gaodaima.com/42576.htmljavamail通过pop3收信,附件下载,我的
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;
/**
*
Title:
*
Description:
*
Copyright: Copyright (c) 2002
*
Company: Socix
* @author simon
* @version 1.0
*/
public class DownloadServlet extends HttpServlet
{
public void service(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
HttpSession session = request.getSession();
String contentType = request.getParameter("contenttype"); //附件的ContentType
int count = Integer.parseInt(request.getParameter("count")); //第几个BodyPart
Object body = session.getAttribute("body"); //邮件的MimeMultipart
if(body != null)
{
System.out.println("download Start!");
try
{
MimeMultipart mp = (MimeMultipart)body;
int i = mp.getCount();
BodyPart part = mp.getBodyPart(count);
contentType = part.getContentType();
String fileName = part.getFileName();
response.setContentType(contentType);
response.setHeader("Content-Disposition","attachment; filename=" + fileName);
System.out.println("download Start!" + part.getSize());
InputStream is = part.getInputStream();
ServletOutputStream out = response.getOutputStream();
BufferedInputStream buf = new BufferedInputStream(is);
byte[] data = new byte[2048];
int readed = buf.read(data);
while (readed != -1)
{
out.write(data,0,readed);
readed = buf.read(data);
}
out.flush();
out.close();
buf.close();
System.out.println("download Over !");
}
catch (Exception ex)
{
System.out.println("error : " + ex);
}
}
}
}
欢迎大家阅读《javamail通过pop3收信,附件下载,我的》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码
微信 赏一包辣条吧~
支付宝 赏一听可乐吧~