⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 downloadattachment.java

📁 jsp+servlet实现的标准MVC系统 jsp-servlet-service-DAO-DBMS
💻 JAVA
字号:
/*
 * 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());
}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -