package com.sxt.servlet;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
public class DownServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//接收前台下载的文件路径名
String url = request.getParameter("url");
response.setContentType("application/x-download");//设置下载的application
String filedisplay=url.split("/")[1];//下载文件时显示文件的保存名称
response.addHeader("Content-Disposition", "attachment;filename="+filedisplay);//下载弹窗
url=request.getServletContext().getRealPath("/")+url;
File f = new File(url);
if(f.exists()){
ServletOutputStream out=response.getOutputStream();
FileUtils.copyFile(f, out);
}
}
}