package com.stone.ms.servlet;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.stone.ms.creater.DocCreater;
/**
* Servlet implementation class DownloadServlet
*/
@WebServlet("/download")
public class DownloadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public DownloadServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 设置response类型
String fileName = "myDownFile.doc";
// 设置响应正文的MIME类型
response.setContentType("Content-Disposition;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;" + " filename=" + fileName);
// 获取数据,设置数据
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("company", "Freemarker");
dataMap.put("companyname", "Freemarker测试有限公司");
dataMap.put("addr", "China Hangzhou");
dataMap.put("contact", "西湖区文一路");
dataMap.put("phone", "123456");
List<Map<String, String>> payList = new ArrayList<>();
for (int i = 1; i < 5; i++) {
Map<String, String> iMap = new HashMap<>();
iMap.put("deadline", "20160" + i + "10");
iMap.put("pay", i * 10 + "");
iMap.put("bk", "10");
iMap.put("own", "100");
payList.add(iMap);
}
dataMap.put("itemlist", payList);
// 把本地文件发送给客户端
Writer out = response.getWriter();
DocCreater dc = new DocCreater();
dc.createDoc(out, dataMap, "demo.ftl");
out.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
评论5
最新资源