package cn.szusst.aqms.action.common;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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 ServletDownload
*/
@WebServlet(asyncSupported = true, urlPatterns = { "/ServletDownload" })
public class ServletDownload extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
/**
@see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/x-msdownload");
// TODO Auto-generated method stub
//獲得請求文件名
String filename = request.getParameter("filename");
//設置文件MIME類型
response.setContentType(getServletContext().getMimeType(filename));
//設置Content-Disposition
response.setHeader("Content-Disposition", "attachment;filename="+filename);
//讀取目標文件,通過response將目標文件寫到客戶端
//獲取目標文件的絕對路徑
// //讀取文件
OutputStream out = response.getOutputStream();
String fullFileName = filename;
InputStream in = new FileInputStream(fullFileName);
//寫文件
int b;
while((b=in.read())!= -1)
{
out.write(b);
}
in.close();
// out.close();
}
/**
}
這是百度上搜的呀