java 代码
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.log4j.Logger;
- public class DownloadRscFileServlet extends HttpServlet {
- private Logger log = Logger.getLogger(this.getClass());
- /**
- * Constructor of the object.
- */
- public DownloadRscFileServlet() {
- super();
- }
- /**
- * Destruction of the servlet. <br>
- */
- public void destroy() {
- super.destroy(); // Just puts "destroy" string in log
- // Put your code here
- }
- /**
- * The doGet method of the servlet. <br>
- *
- * This method is called when a form has its tag value method equals to get.
- *
- * @param request the request send by the client to the server
- * @param response the response send by the server to the client
- * @throws ServletException if an error occurred
- * @throws IOException if an error occurred
- */
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String rscFileName = request.getParameter("rscFileName");
- if(rscFileName == null || rscFileName.equals("")){
- log.debug(
- "Invaild request:can not get type from request!");
- return;
- }
- String path = "d:/upload";
- //String rscFileName = SMPConfig.getValue("");
- File rscFile = new File(path+"/" + rscFileName);
- if(!rscFile.exists()){
- log.debug(
- "In DownloadRscFileServlet..... RscFile does not exist! RscFileName:" +
- rscFileName + " FileName:" + rscFile.getAbsolutePath());
- response.getWriter().println(rscFileName + " does not exist!");
- return;
- }
- response.setHeader("Content-disposition","attachment; filename=" + rscFile.getName());
- response.setContentType("application/x-msdownload");
- OutputStream out = response.getOutputStream();
- FileInputStream in = new FileInputStream(rscFile);
- int i = -1;
- while((i = in.read()) != -1){
- out.write(i);
- }
- in.close();
- out.close();
- }
- /**
- * The doGet method of the servlet. <br>
- *
- * This method is called when a form has its tag value method equals to get.
- *
- * @param request the request send by the client to the server
- * @param response the response send by the server to the client
- * @throws ServletException if an error occurred
- * @throws IOException if an error occurred
- */
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doGet(request,response);
- }
- }