---
title: "下载服务的Servlet"
author: "zaife"
date: "2007-11-07T17:21:47Z"
tags: ["Apache", "Java", "Servlet", "log4j"]
slug: "servlet"
draft: false
lastmod: "2007-11-07T17:21:47Z"
id: 138823
url: "https://zaife.iteye.com/blog/138823"
---

java 代码

01. import java.io.File;
02. import java.io.FileInputStream;
03. import java.io.IOException;
04. import java.io.OutputStream;
05. import javax.servlet.ServletException;
06. import javax.servlet.http.HttpServlet;
07. import javax.servlet.http.HttpServletRequest;
08. import javax.servlet.http.HttpServletResponse;
09. import org.apache.log4j.Logger;
10. public class DownloadRscFileServlet extends HttpServlet {
11. private Logger log = Logger.getLogger(this.getClass());
12. /\**
13. * Constructor of the object.
14. \*/
15. public DownloadRscFileServlet() {
16. super();
17. }
18. /\**
19. * Destruction of the servlet. &lt;br&gt;
20. \*/
21. public void destroy() {
22. super.destroy(); // Just puts "destroy" string in log
23. // Put your code here
24. }
25. /\**
26. * The doGet method of the servlet. &lt;br&gt;
27. \*
28. * This method is called when a form has its tag value method equals to get.
29. \*
30. * @param request the request send by the client to the server
31. * @param response the response send by the server to the client
32. * @throws ServletException if an error occurred
33. * @throws IOException if an error occurred
34. \*/
35. public void doGet(HttpServletRequest request, HttpServletResponse response)
36. throws ServletException, IOException {
37. String rscFileName = request.getParameter("rscFileName");
38. if(rscFileName == null || rscFileName.equals("")){
39. log.debug(
40. "Invaild request:can not get type from request!");
41. return;
42. }
43. String path = "d:/upload";
44. //String rscFileName = SMPConfig.getValue("");
45. File rscFile = new File(path+"/"  + rscFileName);
46. if(!rscFile.exists()){
47. log.debug(
48. "In DownloadRscFileServlet..... RscFile does not exist! RscFileName:" +
49. rscFileName + " FileName:" + rscFile.getAbsolutePath());
50. response.getWriter().println(rscFileName + " does not exist!");
51. return;
52. }
53. response.setHeader("Content-disposition","attachment; filename=" + rscFile.getName());
54. response.setContentType("application/x-msdownload");
55. OutputStream out = response.getOutputStream();
56. FileInputStream in = new FileInputStream(rscFile);
57. int i = -1;
58. while((i = in.read()) != -1){
59. out.write(i);
60. }
61. in.close();
62. out.close();
63. }
64. /\**
65. * The doGet method of the servlet. &lt;br&gt;
66. \*
67. * This method is called when a form has its tag value method equals to get.
68. \*
69. * @param request the request send by the client to the server
70. * @param response the response send by the server to the client
71. * @throws ServletException if an error occurred
72. * @throws IOException if an error occurred
73. \*/
74. public void doPost(HttpServletRequest request, HttpServletResponse response)
75. throws ServletException, IOException {
76. doGet(request,response);
77. }
78. }