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 = "";
	                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();
	                }
	            }
	        }
	}

 

你可能感兴趣的:(java)