DownloadAttachment.java
上传用户:junmaots
上传日期:2022-07-09
资源大小:2450k
文件大小:2k
源码类别:

Jsp/Servlet

开发平台:

Java

  1. /*
  2.  * Created on 2005-12-10
  3.  *
  4.  * TODO To change the template for this generated file go to
  5.  * Window - Preferences - Java - Code Style - Code Templates
  6.  */
  7. package com.mycompany.servlet;
  8. import java.io.IOException;
  9. import javax.servlet.ServletConfig;
  10. import javax.servlet.ServletException;
  11. import javax.servlet.ServletOutputStream;
  12. import javax.servlet.http.HttpServlet;
  13. import javax.servlet.http.HttpServletRequest;
  14. import javax.servlet.http.HttpServletResponse;
  15. import com.mycompany.news.dao.impl.NewsDAOImpl;
  16. import com.mycompany.news.dto.NewsAttachment;
  17. import com.mycompany.news.service.NewsService;
  18. /**
  19.  * @author Administrator
  20.  *
  21.  * TODO To change the template for this generated type comment go to
  22.  * Window - Preferences - Java - Code Style - Code Templates
  23.  */
  24. public class DownloadAttachment extends HttpServlet {
  25. private ServletConfig config;
  26. public void init(ServletConfig arg0) throws ServletException {
  27. config = arg0;
  28. }
  29. /* (non-Javadoc)
  30.  * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
  31.  */
  32. protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1)
  33. throws ServletException, IOException {
  34. // TODO Auto-generated method stub
  35. service(arg0, arg1);
  36. }
  37. /* (non-Javadoc)
  38.  * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
  39.  */
  40. protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1)
  41. throws ServletException, IOException {
  42. // TODO Auto-generated method stub
  43. service(arg0, arg1);
  44. }
  45. protected void service(HttpServletRequest request, HttpServletResponse response)
  46. throws ServletException, IOException {
  47. Long attid = Long.valueOf(request.getParameter("id"));
  48. NewsService service = new NewsService();
  49. service.setNewsDAO(new NewsDAOImpl());
  50. NewsAttachment attachment = service.loadAttachment(attid.longValue());
  51. // String mimeType = getServletContext().getMimeType(attachment.getAttachmentName());
  52. // if(mimeType!=null)
  53. // response.setContentType(mimeType);
  54. response.setHeader("Content-Disposition","attachment;filename="+attachment.getAttachmentName());
  55. ServletOutputStream out = response.getOutputStream();
  56. System.out.println("attachment.getAttContent() = "+attachment.getAttachmentContent());
  57. out.write(attachment.getAttachmentContent());
  58. }
  59. }