js请求服务器下载文档(doc、wps、txt等到本地)

js请求代码

downloadTemplate('./auditreport/exportWPS.do?filename='+encodeURIComponent(returnInfo[2]), 'filename', 'test');

/**
             * 用于下载导入模板时的影藏form表单的提交,采用post方式提交
             * @param action 请求后台方法
             * @param type 文件类型
             * @param value 文件名
             */
            function downloadTemplate(action, type, value){
                var form = document.createElement('form');
                document.body.appendChild(form);
                form.style.display = "none";
                form.action = action;
                form.id = 'wps';
                form.method = 'post';
                var newElement = document.createElement("input");  
                newElement.setAttribute("type","hidden");  
                newElement.name = type;
                newElement.value = value;
                form.appendChild(newElement); 
                form.submit();
                loaderHide();
            }

后台代码

	@RequestMapping("/exportWPS.do")
	public void getExportFile(HttpServletRequest request,HttpServletResponse resp) {
		 URL basePath = WordUtils.class.getClassLoader().getResource("");
	     String separator = File.separator;
	     String filename = basePath.getPath() + separator +request.getParameter("filename");
	     
	        DataInputStream in = null;
	        OutputStream out = null;
	        try{
	            resp.reset();// 清空输出流
	            
	            String resultFileName = System.currentTimeMillis() + ".wps";
	            resultFileName = URLEncoder.encode(resultFileName,"UTF-8");  
	            resp.setCharacterEncoding("UTF-8");  
	            resp.setHeader("Content-disposition", "attachment; filename=" + resultFileName);// 设定输出文件头
	            resp.setContentType("application/msexcel");// 定义输出类型
	            //输入流:本地文件路径
	            in = new DataInputStream(
	                    new FileInputStream(new File(filename)));  
	            //输出流
	            out = resp.getOutputStream();
	            //输出文件
	            int bytes = 0;
	            byte[] bufferOut = new byte[1024];  
	            while ((bytes = in.read(bufferOut)) != -1) {  
	                out.write(bufferOut, 0, bytes);  
	            }
	        } catch(Exception e){
	            e.printStackTrace();
	            resp.reset();
	            try {
	                OutputStreamWriter writer = new OutputStreamWriter(resp.getOutputStream(), "UTF-8");  
	                String data = "<script language='javascript'>alert(\"\\u64cd\\u4f5c\\u5f02\\u5e38\\uff01\");</script>";
	                writer.write(data); 
	                writer.close();
	            } catch (IOException e1) {
	                e1.printStackTrace();
	            }
	        }finally {
	            if(null != in) {
	                try {
	                    in.close();
	                } catch (IOException e) {
	                    e.printStackTrace();
	                }
	            }
	            if(null != out) {
	                try {
	                    out.close();
	                } catch (IOException e) {
	                    e.printStackTrace();
	                }
	            }
	        }
	}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值