RequestDispatcher实现文件下载

本来我使用的是文件流下载的方式,在Tomcat下可行,但是在WebLogic下使用文件流下载下来的文件总是乱码,最终我妥协了,选择了RequestDispatcher方式。
1.文件流方式
js中的文件请求:window.location.href = "download.jsp?filepath="+encodeURI(responseArray.path);//filepath中含有中文
实现下载的download.jsp文件如下:
<%@ page language="java" pageEncoding="utf-8" %>
<%@ page contentType="application/x-msdownload" %>
<%@ page language="java" import="java.io.*"%>
<%@ page language="java" import="java.net.URLEncoder"%>
<%@ page language="java" import="java.util.*"%>
<%
      String file=request.getParameter("filepath");
      String filepath=new String(file.getBytes("ISO8859-1"),"utf-8" );
      response.setContentType("application/x-msdownload; CHARSET=utf-8");
      String displaypath=filepath.substring(filepath.lastIndexOf("/")+1);
      response.addHeader("Content-Disposition","attachment;filename=" + new String(displaypath.getBytes("gb2312"), "ISO8859-1" ));
      String filepathdownload=this.getServletConfig.getServletContext().getRealPath(filepath);
      OutputStream outputStream=null;
      FileInputStream inputStream =null;
      try{
               outputStream = response.getOutputStream();
               inputStream = new FileInputStream(filepathdownload);
               byte[] buffer = new byte[1024];
               int i = -1;
               while ((i =inputStream.read(buffer)) != -1)
               {
                     outputStream.write(buffer, 0, i);
               }
               outputStream.flush();
               //下面这两句在Tomcat下需要,在Weblogic下要去掉,否则出错
               out.clear();
               out = pageContext.pushBody();
      }catch(Exception e)
      {
               System.out.println("Error!");
               e.printStackTrace();
      }
      finally{
               if(inputStream!=null)
               {
                   inputStream.close();
                   inputStream=null;
               }
              if(outputStream!=null)
              {
                   outputStream.close();
                   outputStream=null;
              }
      }
%>
2.RequestDispatcher实现文件下载
<%@page language="java" import="java.net.*" pageEncoding="gb2312"%>
<%
    //如果是从前台传过来的参数就这样写
    //String filePath = "/"+request.getParameter("path");前面红字标识的“/”不能没有  
    //String fileName = request.getParameter("name"); 
    response.setContentType("application/x-download");//设置为下载application/x-download
    String filenamedownload = "/Word/作文.doc";//即将下载的文件的相对路径
    String filenamedisplay = "作文.doc";//下载文件时显示的文件保存名称
    filenamedisplay = URLEncoder.encode(filenamedisplay,"UTF-8");
    response.addHeader("Content-Disposition","attachment;filename=" + filenamedisplay);   
    try
    {
        RequestDispatcher dispatcher = application.getRequestDispatcher(filenamedownload);
        if(dispatcher != null)
        {
            dispatcher.forward(request,response);
        }
        response.flushBuffer();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }   
%>
注意:
1.String filenamedownload = "/Word/作文.doc";指的是即将下载的文件的相对路径,表示作文.doc文件放在工程下的Word文件夹内,一定要注意"/Word/作文.doc",Word前面一定要有“/”,不能写成String filenamedownload = "Word/作文.doc";
2.RequestDispatcher下载方式更为简单,但是缺点在于:下载的文件必须放在工程的内部,application.getRequestDispatcher()方法的参数也必须是相对路径

你可能感兴趣的:(dispatcher)