easyui 下载 服务器文件的实现

<a></a>标签如果指定一个文件,并且文件浏览器不能打开,浏览器将执行下载的功能,如果能打开,就会在浏览器打开

而我要实现的是服务端的下载,在WebContext下创建files文件夹,存放文件

 

Controller层用的是springMVC

效果图如下

 1.html代码

<div id="showDownload" class="easyui-dialog" title="下载文件" closed="true"
		buttons="#showDownload-buttons">
		
		<div id="downDIV" class="easyui-panel" style="width:400px;padding:30px 70px 50px 70px">
			<h1>实验报告下载</h1>
			<div id="downs">
			</div>
		</div>
		<div id="showDownload-buttons">
			<a href="javascript:void(0)" class="easyui-linkbutton"
				iconCls="icon-cancel"
				onclick="javascript:$('#showDownload').dialog('close')" style="width: 90px">关
				闭</a>
		</div>
	</div>

2.js代码

//下载弹出框
	function downloads() {
		$('#downs').empty();
		var row = $('#tb').datagrid('getSelected');//获取选中行
		if (!row) {
			$.messager.alert("提示", "请选择您想要上传的预约!");
			return;
		}
		if (row.cancelOrder != 3 ) {
			$.messager.alert("提示", "预约还未完成!");
			return;
		}
		$.ajax({ 
			type:"post",  //提交方式    
            dataType: 'json',
			url: '${pageContext.request.contextPath}/order/getPaths.action', 
			data:{id:row.id},
			success: function(map){
				if (map.msg == "1") {
					var list = map.list;
					for(var i = 0; i < list.length;i++){
						$('#downs').append("<a id='download"+i+"' href='${pageContext.request.contextPath}/order/download.action?fileName="+list[i]+"'>实验报告</a></br></br>");
					}
				} else {
					$.messager.alert("操作提示", "下载失败:" + map.msg);
				}
	      	},
	      	error : function(XMLHttpRequest, textStatus, errorThrown) {
				$.messager.alert("错误提示", "失败!" + textStatus);
			}
		});
		$('#showDownload').dialog('open').dialog('center');
	}

3.后台控制层

/*
	 * 下载文件
	 */
	@RequestMapping("/download")
	@ResponseBody
	public void download(HttpServletResponse response,HttpServletRequest request) {
		String filename =request.getParameter("fileName");
	    orderService.doGetFile(request, response ,filename);
	}

4.业务层

//下载
	@Override
	public void doGetFile(HttpServletRequest request, HttpServletResponse response, String filename) {
       try {
    	   //得到要下载的文件名
           String fileName = filename;
           String fileSaveRootPath=request.getSession().getServletContext().getRealPath("/files");
           //文件路径
           File file = new File(fileSaveRootPath + "\\" + fileName);
           //如果文件不存在
           if(!file.exists()){
               System.out.println("message您要下载的资源已被删除!!");
               return;
           }
           //设置响应头,控制浏览器下载该文件
           response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
    	   InputStream fis = new BufferedInputStream(new FileInputStream(fileSaveRootPath + "\\" + fileName));
           byte[] buffer = new byte[fis.available()]; 
           fis.read(buffer);  //读取文件流
           fis.close();//关闭文件流
           response.reset();//重置结果集
           response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
           response.addHeader("Content-Length", "" + file.length());  //返回头 文件大小
           response.setContentType("application/octet-stream");    //设置数据种类
           ///获取返回体输出权
           OutputStream os = new BufferedOutputStream(response.getOutputStream());  
           // 输出文件
           os.write(buffer);
           os.flush();
           os.close();
       } catch (IOException e) {
    	   e.printStackTrace();
       } 
	}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值