DownloadAttachment.java
上传用户:junmaots
上传日期:2022-07-09
资源大小:2450k
文件大小:2k
- /*
- * Created on 2005-12-10
- *
- * TODO To change the template for this generated file go to
- * Window - Preferences - Java - Code Style - Code Templates
- */
- package com.mycompany.servlet;
- import java.io.IOException;
- import javax.servlet.ServletConfig;
- import javax.servlet.ServletException;
- import javax.servlet.ServletOutputStream;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import com.mycompany.news.dao.impl.NewsDAOImpl;
- import com.mycompany.news.dto.NewsAttachment;
- import com.mycompany.news.service.NewsService;
- /**
- * @author Administrator
- *
- * TODO To change the template for this generated type comment go to
- * Window - Preferences - Java - Code Style - Code Templates
- */
- public class DownloadAttachment extends HttpServlet {
-
- private ServletConfig config;
- public void init(ServletConfig arg0) throws ServletException {
- config = arg0;
- }
- /* (non-Javadoc)
- * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
- */
- protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1)
- throws ServletException, IOException {
- // TODO Auto-generated method stub
- service(arg0, arg1);
- }
- /* (non-Javadoc)
- * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
- */
- protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1)
- throws ServletException, IOException {
- // TODO Auto-generated method stub
- service(arg0, arg1);
- }
- protected void service(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- Long attid = Long.valueOf(request.getParameter("id"));
- NewsService service = new NewsService();
- service.setNewsDAO(new NewsDAOImpl());
- NewsAttachment attachment = service.loadAttachment(attid.longValue());
- // String mimeType = getServletContext().getMimeType(attachment.getAttachmentName());
- // if(mimeType!=null)
- // response.setContentType(mimeType);
- response.setHeader("Content-Disposition","attachment;filename="+attachment.getAttachmentName());
- ServletOutputStream out = response.getOutputStream();
- System.out.println("attachment.getAttContent() = "+attachment.getAttachmentContent());
- out.write(attachment.getAttachmentContent());
- }
- }