javaweb:設置mime內容類型運行servlet下載文件,用客戶端Excel打開
來源:我的學習筆記


代碼:
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
?* Servlet implementation class ExcelServlet
?*/
@WebServlet(name="ExcelServlet",urlPatterns = "/excel.do")
public class ExcelServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
? ? ? ?
? ? /**
? ? ?* @see HttpServlet#HttpServlet()
? ? ?*/
? ? public ExcelServlet() {
? ? ? ? super();
? ? ? ? // TODO Auto-generated constructor stub
? ? }
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("application/vnd.ms-excel;charset=gb2312");/* 設置MIME內容 */
PrintWriter out=response.getWriter();
out.println("學號\t姓名\t性別\t年齡\t所在專業(yè)");//用制表符制表
out.println("0001\t張三\t男\(zhòng)t11\t食品加工");
out.println("0002\t李四\t男\(zhòng)t12\t藝術管理");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}

