jsp 下载 服务端的文件到本地

本文详细介绍了在Web环境中实现文件下载的方法,包括通过JS调用Servlet进行下载和使用文件流输出两种方式。讨论了每种方法的优缺点,并提供了关键代码示例。文章还强调了在设置响应头时应注意的事项,如防止浏览器缓存、处理中文文件名等,以确保文件能够正确地下载到客户端。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、list页面的js,点击list列表下面的下载按钮调用js

function downloadDoc(filePath,fileName){
	var path = filePath+fileName;
	var contextLength = "<%=request.getContextPath()%>";
	var sp = path.substring(contextLength.length,path.length);
	document.getElementById("path").value = sp;
	document.getElementById("fileName").value = fileName;
	//down_frame.location.href = path;decodeURI(path);encodeURIComponent
	var sForm1 = document.form1;
	sForm1.action = "<%=request.getContextPath()%>/com/icss/mdm/usermanual/servlet/StandardDocDownServlet";
	sForm1.submit();
   
}


下面是 一个form表单

<form id="form1" name="form1" method="post">
	<input type="hidden" id="path" name="path"  />
	<input type="hidden" id="fileName" name="fileName"  />
</form>

通过servlet的代码如下:
package com.icss.mdm.usermanual.servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.icss.mdm.base.servlet.BaseServlet;
import com.icss.pangu.logging.Log;
import com.icss.pangu.logging.LogFactory;

public class StandardDocDownServlet extends BaseServlet {
    private static Log log = LogFactory.getLog(StandardDocDownServlet.class);
    
    public void performTask(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String path = getParameter(request, "path", true, true, false, "");
        String fileName = getParameter(request, "fileName", true, true, false, "");
        request.setAttribute("path", path);
        request.setAttribute("fileName", fileName);
        if(!"".equals(path) && !"".equals(fileName)){//去函数说明文档页面
            getServletContext().getRequestDispatcher("/mdm/jsp/usermanual/DownFile.jsp").forward(request,response);
        }
       
    }
}


下面是下载的页面:

<%@ page contentType="text/html;charset=GBK"%>
<%@ page import="java.net.URLEncoder"%>

<html>
<title>下载标准文档</title>

<script language="javascript">
</script>
<body topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0">
<%
    //去除缓存 
    /**response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
    String contextPath = request.getContextPath();**/
    
    //String path = request.getParameter("path");
    //String fileName = request.getParameter("fileName");
    String path = (String)request.getAttribute("path");//即将下载的文件的相对路径
    String fileName = (String)request.getAttribute("fileName");//下载文件时显示的文件保存名称
  	
    response.setContentType("application/x-download");//设置为下载application/x-download
    String filedownload = path;//即将下载的文件的相对路径
    String filedisplay = fileName;//下载文件时显示的文件保存名称
    String filenamedisplay = URLEncoder.encode(filedisplay,"utf-8");
    response.addHeader("Content-Disposition","attachment;filename=" + filenamedisplay);
     
    try{
    <span style="white-space:pre">	</span>RequestDispatcher dis = application.getRequestDispatcher(filedownload);
    <span style="white-space:pre">	</span>if(dis!= null) {
    <span style="white-space:pre">	</span>     dis.forward(request,response);
    <span style="white-space:pre">	</span>}
    <span style="white-space:pre">	</span>response.flushBuffer();
    } catch(Exception e) {
    <span style="white-space:pre">	</span>e.printStackTrace();
    } finally{
     
    }
%>
</body>
</html>

二、采用文件流输出的方式下载 
<%@ page language="java" contentType="application/x-msdownload" pageEncoding="gb2312"%>
<%
  //关于文件下载时采用文件流输出的方式处理:
  //加上response.reset(),并且所有的%>后面不要换行,包括最后一个;

  response.reset();//可以加也可以不加
  response.setContentType("application/x-download");

//application.getRealPath("/main/mvplayer/CapSetup.msi");获取的物理路径

  String filedownload = "想办法找到要提供下载的文件的物理路径+文件名";
  String filedisplay = "给用户提供的下载文件名";
  String filedisplay = URLEncoder.encode(filedisplay,"UTF-8");
  response.addHeader("Content-Disposition","attachment;filename=" + filedisplay);

  try {
<pre name="code" class="html">  <span style="white-space:pre">	</span>java.io.OutputStream outp = <span style="font-family: Arial, Helvetica, sans-serif;">response.getOutputStream();</span>
  <span style="white-space:pre">	</span>java.io.FileInputStream in = <span style="font-family: Arial, Helvetica, sans-serif;">new FileInputStream(filenamedownload);</span>

  <span style="white-space:pre">	</span>byte[] b = new byte[1024];
  <span style="white-space:pre">	</span>int i = 0;

 <span style="white-space:pre">	</span>while((i = in.read(b)) > 0) {
  <span style="white-space:pre">		</span>outp.write(b, 0, i);
  <span style="white-space:pre">	</span>}  
<span style="white-space:pre">	</span>outp.flush();
<span style="white-space:pre">	</span>//要加以下两句话,否则会报错
<span style="white-space:pre">	</span>//java.lang.IllegalStateException: getOutputStream() has already been called for //this response  
<span style="white-space:pre">	</span>out.clear();
<span style="white-space:pre">	</span>out = pageContext.pushBody();
  } catch(Exception e) {
  <span style="white-space:pre">	</span>System.out.println("Error!");
  <span style="white-space:pre">	</span>e.printStackTrace();
  } finally {
  <span style="white-space:pre">	</span>if(in != null)
  <span style="white-space:pre">	</span>{
  <span style="white-space:pre">		</span>in.close();
  <span style="white-space:pre">		</span>in = null;
  <span style="white-space:pre">	</span>}
  
<span style="white-space:pre">	</span>if(outp != null) {
  <span style="white-space:pre">		</span>outp.close();
  <span style="white-space:pre">		</span>outp = null;
  <span style="white-space:pre">	</span>}
  }
%>
备注:如何出现java.io.IOException: response already committed这个错误 则 把out.clear();out = pageContext.pushBody();这两个注释掉就可以解决了。

对于第二种方法,我认为应该是比较常用的。不过有几个地方是值得我们注意的: 

一、采用第二种方法的主要优点是实际文件的存放路径对客户端来说是透明的。 
这个文件可以存在于任何你的服务器能够取得到的地方,而客户端不一定能直接得到。例如文件来自于数据库或者内部网络的一个FTP服务器。还句话说,这种方式可以实现隐藏实际文件的URL地址。 


二、为了防止客户端浏览器直接打开目标文件(例如在装了MS Office套件的Windows中的IE浏览器可能就会直接在IE浏览器中打开你想下载的doc或者xls文件),你必须在响应头里加入强制下载的MIME类型: 
response.setContentType("application/force-download");//设置为下载application/force-download 
这样,就可以保证在用户点击下载链接的时候浏览器一定会弹出提示窗口来询问你是下载还是直接打开并允许你选择要打开的应用程序,除非你设置了浏览器的一些默认行为。 
或者,你想让客户端自行处理各种不同的文件类型,你可以在服务器的配置文件中配置MIME类型映射,通过简单的判断文件后缀名来处理。例如,在Tomcat中设置MIME响应类型: 
如果文件在客户端中的响应程序类型和期望不一致,修改$TOMCAT_HOME\conf\web.xml文件中的如下部分 : 
<mime-mapping> 
  <extension>zip</extension> 
  <mime-type>application/zip</mime-type> 
</mime-mapping> 
<mime-mapping> 
  <extension>mht</extension> 
  <mime-type>message/rfc822</mime-type> 
</mime-mapping> 
…… 


三、在响应头中尽量不要设置浏览器缓存期限。 
有时候用户在点击了下载链接后,在弹出窗口中,用户想直接点击“打开”,而不想保存到指定路径。这时候如果我们在响应头中限制了不允许使用浏览器缓存(即总是刷新),在IE浏览器中我们将无法直接打开该文件。因为限制了不允许使用缓存,浏览器无法将文件保存到临时文件夹(即缓存)。 
也就是说,在响应头中不要进行如下的设置(已注释): 
  //response.addHeader("pragma","NO-cache"); 
  //response.addHeader("Cache-Control","no-cache"); 
  //response.addDateHeader("Expries",0); 


四、文件名为中文或其他unicode字符时的处理。 
有时候提供下载的文件名中包含中文字符或者其他unicode字符,会导致浏览器无法正确的采用默认的文件名保存文件。我们应该记住在响应头中包含filename字段并采用ISO8859-1编码(推荐)或者采用UTF-8编码: 
response.setHeader("Content-disposition","attachment; filename="+new String(filename.getBytes("UTF-8"),"iso8859-1")); //采用ISO8859-1编码 
response.setHeader("Content-disposition","attachment; filename="+URLEncoder.encode(filename, "UTF-8")); //采用UTF-8编码 
但是,这种方式在不同的浏览器中表现也有所不同。例如在IE和Firefox中,采用ISO8859-1编码可以正确显示文件名,而在Opera中不管采用那种编码,默认保存的文件名都无法做到正确显示。 
所以最好的方法其实就是尽量在文件名中使用ascii编码。 


五、由于采用流的方式进行输入输出,我们必须保证在使用完毕后关闭流的资源。 
一般我们把关闭流的操作放在finally块中,以保证在程序段结束前一定会关闭流的资源: 


InputStream is = null; 
ServletOutputStream sos = null; 
try { 
  is = ...; //通过某种方式读进数据到输入流 
  sos = response.getOutputStream(); //打开输入流 
  byte[] buff = new byte[2048]; 
  int bytesRead; 
  while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) { 
  sos.write(buff,0,bytesRead); 
  sos.flush(); 
  } 
} catch(IOException ex) { 
  //TODO something with IOException 
} catch(Exception ex) { 
  //TODO something with Exception 
} finally { 
  if(is != null) { 
  is.close(); //关闭输入流 
  } 
  if(sos != null) { 
  sos.close(); //关闭输入流 
  } 

转自:http://blog.youkuaiyun.com/arui_email/article/details/9041283

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值