文件下载:固定文件下载

文件下载原理为数据传输,将服务器上的文件以流的方式发送到客户端,具体方法有多种,本处为下载指定目标文件模板,填写后上传导入数据,本文描述文件下载部分。

本项目下载的模板在WebRoot下,按需修改文件路径。

步骤:获取File路径;判断是否存在;设置响应头,用Stream进行输出。

本处下载文件会产生问题,当数据量很大时,下载文件使用Office无法打开,但使用WPS可以打开,此问题待解决。

1.jsp:按键button

 

<input type="button" value="下载模板" onclick="downloadTemplate()"
		style="background-color: #004B97;color:white;font-family:SimSun;
   font-size:10px;width: 10%;height: 5%;" />onclick="downloadTemplate()"
		style="background-color: #004B97;color:white;font-family:SimSun;
   font-size:10px;width: 10%;height: 5%;" />

2. JavaScript 函数

 

 

//下载模板
	function downloadTemplate() {
		window.open(parent.formateUrl("downTem.xls(访问url)", "_self"));
		$.messager.show({
			title : '提示',
			msg : "导出成功!",
			showType : 'show',
			timeout : 1000,
			style : {
				right : '',
				top : document.body.scrollTop
						+ document.documentElement.scrollTop,
				bottom : ''
			}
		}); 
	}window.open(parent.formateUrl("downTem.xls(访问url)", "_self"));
		$.messager.show({
			title : '提示',
			msg : "导出成功!",
			showType : 'show',
			timeout : 1000,
			style : {
				right : '',
				top : document.body.scrollTop
						+ document.documentElement.scrollTop,
				bottom : ''
			}
		}); 
	}

3.Conntroller

 

获取路径:request.getSession().getServletContext().getRealPath("")

 

	/**
	 * 下载模板
	 * 
	 */
	@RequestMapping("/downTem.xls")
	public void downTem(HttpServletRequest request, HttpServletResponse res) {
		try{
			String path=request.getSession().getServletContext().getRealPath("");
			String fileName="模板.xlsx";
			String filePath=path+"\\"+fileName;
			System.out.println(filePath);
			FileUtil.downloadFile(filePath, fileName, "xlsx", res);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}request.getSession().getServletContext().getRealPath("");
			String fileName="模板.xlsx";
			String filePath=path+"\\"+fileName;
			System.out.println(filePath);
			FileUtil.downloadFile(filePath, fileName, "xlsx", res);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

4.public static void downloadFile(String filePath,String fileName,String fileType,HttpServletResponse response)

public static void downloadFile(String filePath,
			String fileName,String fileType,HttpServletResponse response)
					throws Exception{
		File file=new File(filePath);
		if("xlsx".equals(fileType)){
			response.setContentType("application/vnd.ms-excel;charset=utf-8");
		}
		response.setHeader("content-disposition", 
				"attachment;filename="+URLEncoder.encode(fileName, "UTF-8"));
		response.setContentLength((int)file.length());
		byte[] buffer=new byte[1024];
		BufferedInputStream in=null;
		BufferedOutputStream out=null;
		try {
			in=new BufferedInputStream(new FileInputStream(file));
			out=new BufferedOutputStream(response.getOutputStream());
			int len = 0;
			while ((len = in.read(buffer)) > 0) {
				out.write(buffer,0,len);
			}
			out.flush();
			response.flushBuffer();
		} catch (Exception e) {
			throw new Exception("downloadFile error.");
		} finally{
			if(in!=null){
				in.close();
			}
			if(out!=null){
				out.close();
			}
		}
	}BufferedInputStream in=null;
		BufferedOutputStream out=null;
		try {
			in=new BufferedInputStream(new FileInputStream(file));
			out=new BufferedOutputStream(response.getOutputStream());
			int len = 0;
			while ((len = in.read(buffer)) > 0) {
				out.write(buffer,0,len);
			}
			out.flush();
			response.flushBuffer();
		} catch (Exception e) {
			throw new Exception("downloadFile error.");
		} finally{
			if(in!=null){
				in.close();
			}
			if(out!=null){
				out.close();
			}
		}
	}

 

 

 

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值