ITeye博客

下载服务的Servlet

zaife · 2007-11-07 17:21 · java · Java · Apache · log4j · Servlet · Markdown

java 代码

  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5. import javax.servlet.ServletException;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. 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. <br>
  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. <br>
  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. <br>
  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. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。