• 文件下载


    //下载
    $('#dasyszTable').on('click', '.download', function (e) {
    	
    	var nRow = $(this).parents('tr')[0];
    	var aData = dasyszTable.fnGetData(nRow);
    	//插入日志
    	handleInsertRecord("进入法律、法规制度列表--附件下载");
    	
    	var myform = $("<form></form>");
    	myform.attr('method','post');
    	myform.attr('action',BASEPATH + "flfgsysz/downloadFile");
    	
    	var file1 = $(this).find("a").eq(0).context.innerHTML;
    	var fileName = $("<input type='hidden' name='fileName' />");
    	fileName.attr('value',file1+","+aData.disc+","+aData.num);
    	
    	myform.append(fileName);
    	myform.appendTo('body').submit();
    });
    
    @ResponseBody
    @RequestMapping(value = "/downloadFile", method = RequestMethod.POST)
    public void downloadFile(HttpServletRequest request, HttpServletResponse response) throws Exception {
    	
    	String fileName = request.getParameter("fileName");
    	String[] split = fileName.split(",");
    	String filePath = AddressUtil.getUpload_address() + split[1] + "\\" + split[2] + "_" + split[0];
    	// 读到流中
    	InputStream inStream = new FileInputStream(filePath);// 文件的存放路径
    	// 设置输出的格式
    	response.reset();
    	response.setContentType("bin");
    	response.setContentType("application/octet-stream; charset=utf-8");
    	
    	fileName = URLEncoder.encode(split[0], "UTF-8");
    	if (fileName.length() > 150) {  
    		String guessCharset = "gb2312"; /*根据request的locale 得出可能的编码,中文操作系统通常是gb2312*/  
    		fileName = new String(fileName.getBytes(guessCharset), "ISO8859-1");   
    	}  
    	response.setHeader("Content-Disposition", "attachment; filename=" + fileName);  
    	
    	// 循环取出流中的数据
    	byte[] b = new byte[100];
    	int len;
    	try {
    		while ((len = inStream.read(b)) > 0)
    			response.getOutputStream().write(b, 0, len);
    		inStream.close();
    	} catch (IOException e) {
    		e.printStackTrace();
    	}
    }
    
  • 相关阅读:
    markdown基础
    img alt与title的区别
    关于echarts 报错 初始化对象未定义
    easyui datagrid 右边框被隐藏
    ie 浏览器文本输入框和密码输入框的默认样式
    谷歌内核浏览器 iframe内容的 tab切换 滚动条消失
    导入sass文件
    嵌套规则
    关于ruby -gem无法切换淘宝源
    sass 变量
  • 原文地址:https://www.cnblogs.com/sunBinary/p/11806147.html
Copyright © 2020-2023  润新知