统一的下载页面

1. 页面下载文件,通过download.jsp下载,可以避免直接下载带来的IE6下的卡死现象和一些乱码问题

String path = request.getParameter("path");
	String fullpath = request.getParameter("fullpath");
	
	
	File file = null;
	if(StringUtils.isNotEmpty(path)) {
		file = new File(request.getRealPath(path));
		response.setHeader("Content-Disposition","attachment;filename = " + URLEncoder.encode(StringUtils.substringAfterLast(path, "/"), "UTF-8") );
	} else if(StringUtils.isNotEmpty(fullpath)) {
		file = new File(fullpath);
		response.setHeader("Content-Disposition","attachment;filename = " + URLEncoder.encode(StringUtils.substringAfterLast(fullpath, "/"), "UTF-8") );
	}
        //再获取outputstream前执行这2句话,否则后台报错。
	out.clear();
	out = pageContext.pushBody();
	OutputStream os = response.getOutputStream();
	IOUtils.write(FileUtils.readFileToByteArray(file), os);
	os.flush();
	os.close();  

 2.调用过程:

   (1)

var elemIframe = document.createElement("iframe");  
elemIframe.src = __ctxPath+"/../download.jsp?id="+id;  
elemIframe.style.display = "none";  
document.body.appendChild(elemIframe);

   (2)

<a target="_blank" href="'+__ctxPath + '../download.jsp?path=/../test.xls">

 

你可能感兴趣的:(下载)