使用servlet来下载文件
下载的文件excel表格
package servlets;
import java.io.FileInputStream;
import java.io.OutputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FileDownload extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) {
try {
//要下载的文件类型是xls表格
String fname = "test.xls";
//对字符编码进行设置,用来支持中文的文件名
response.setCharacterEncoding("UTF-8");
fname = java.net.URLEncoder.encode(fname, "UTF-8");
response.setHeader("Content-Disposition", "attachment; filename="+fname);
response.setContentType("application/msexcel");// 定义输出类型
} catch (Exception e) {
System.out.println(e);
}
}
}