Extjs4.0实现文件下载

来源:互联网 发布:win ubuntu 双系统 编辑:程序博客网 时间:2024/06/06 17:19

Extjs4.0实现文件下载

1、前台:{
                    text: "用户手册",
                    id: "yhsc",
                    icon: 'images/ico/common/help.png',
                    handler: function() {
                      window.open("fileServlet","用户手册").focus();
                     }
                }

2、后台(以servlet为例)

  1》web.xml文件配置:

  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>FileServlet</servlet-name>
    <servlet-class>com.gistone.util.FileServlet</servlet-class>
  </servlet>

 <servlet-mapping>
    <servlet-name>FileServlet</servlet-name>
    <url-pattern>/fileServlet</url-pattern>
  </servlet-mapping>

2》FileServlet.java文件配置:

package com.gistone.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FileServlet extends HttpServlet {
 private static final String RJD_ExcelTemplate_URL = "use/userManual.pdf";  //配置文件所在路径
 /**
  * Constructor of the object.
  */
 public FileServlet() {
  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 {
  doPost(request,response);
 }

 /**
  * The doPost method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to post.
  *
  * @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 {
  try{
   File f = new File(request.getRealPath("/") + RJD_ExcelTemplate_URL);
   FileInputStream input = new FileInputStream(f);
   response.reset();
   response.setContentType("application/pdf;charset=UTF-8");
   response.addHeader("Content-Disposition", "attachment;filename=\"" + new String(RJD_ExcelTemplate_URL.substring(RJD_ExcelTemplate_URL.lastIndexOf("/")+1).getBytes("GBK"), "ISO8859_1") + "\"");
   OutputStream out = response.getOutputStream();
   byte[] b = new byte[1024];
   int len = 0;
   while ((len = input.read(b)) != -1) {
    out.write(b, 0, len);
   }
   }catch(FileNotFoundException e1)
    {
     System.out.println("没有找到您要的文件");
    }
    catch(Exception e)
    {
     System.out.println("系统错误,请及时与管理员联系");
    }

 }

 /**
  * Initialization of the servlet. <br>
  *
  * @throws ServletException if an error occurs
  */
 public void init() throws ServletException {
  // Put your code here
 }

}

 

0 0
原创粉丝点击