从服务器上下载文件

1、jsp

<span style="font-size:12px;"><%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> 
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta charset="utf-8">
<title></title>
<meta name="keywords" content="">
<meta name="description" content="">
<meta name="author" content="">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="-1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="">
</head>
<script src="<%=request.getContextPath()%>/man/js/ajaxfileupload.js"></script>
<%@ include file="/WEB-INF/views/treeogrist.jsp"%>
<input type="hidden" id="treeCZTypeId">
<body >
   <div class="page005">
       <div class="row">
           <div class="col-xs-9">
               <div class="panel panel-ui">
                   <div class="panel-heading">
                       <form class="form-inline">
                           <div class="form-group">
                               <a href="<%=request.getContextPath()%>/download/excel.html?fileName=团队激励宝-产值事件下载模板.xls" 
                                   target=" _blank" class="download action">下载事件模板</a>
                           </div>
                       </form>
                   </div>
               </div>
           </div>
       </div>
   </div>
</body>
</html>

</span>
2、controller

<span style="font-size:12px;">package system;

@Controller
@RequestMapping(value="/download")
public class DownloadController {
	private Logger log = Logger.getLogger(DownloadController.class);

	@RequestMapping(value="excel")
	public void excel(ModelAndView mav,HttpServletRequest request,HttpServletResponse response) {
		final String userAgent = request.getHeader("USER-AGENT");
		String fileName =request.getParameter("fileName");
		if(StringUtils.isBlank(fileName)){
			return;
		}
		
		 InputStream inputStream = null;  
	     OutputStream outputStream = null; 
	     try {  
	    	 String finalFileName = null;
	    	 File f = new File(this.getClass().getResource("").getPath()); 
			String filepath = f.toString().split("WEB-INF")[0]+"excelmodel"+File.separator+fileName;  
//			System.out.println(filepath); 
	        File file = new File(filepath);  
	        byte[] b= new byte[1024];  
	        int len = 0;  
            inputStream = new FileInputStream(file);  
            outputStream = response.getOutputStream();  
              
            response.setContentType("application/force-download");  
            if(StringUtils.contains(userAgent, "MSIE")){//IE浏览器
                finalFileName = URLEncoder.encode(fileName,"UTF8");
            }else if(StringUtils.contains(userAgent, "Mozilla")){//google,火狐浏览器
                finalFileName = new String(fileName.getBytes(), "ISO8859-1");
            }else{
                finalFileName = URLEncoder.encode(fileName,"UTF8");//其他浏览器
            }
    		String Filename = finalFileName.substring(0, finalFileName.length()-4)+
    				(new SimpleDateFormat("yyyyMMdd").format(new Date()))+".xls";
            response.addHeader("Content-Disposition","attachment; filename=" + Filename);  
            response.setContentLength( (int) file.length( ) );  
              
            while((len = inputStream.read(b)) != -1){  
                outputStream.write(b, 0, len);  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        }finally{  
            if(inputStream != null){  
                try {  
                    inputStream.close();  
                    inputStream = null;  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
            if(outputStream != null){  
                try {  
                    outputStream.close();  
                    outputStream = null;  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
        }  
	}
}
</span>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雨润泽林

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值