文件下载例子

 /**
     * 检索列表中下载附件
     * 传入参数:@param uploadFile
     * 传入参数:@param request
     * 返回类型:void
     */
    @RequestMapping(value = "/downloadFile")
    public void downloadFile(UploadFile uploadFile,HttpServletRequest request, HttpServletResponse response) {
		BufferedInputStream in = null;  
		BufferedOutputStream out = null; 
		FileInputStream fis=null;
		try {  
			/*根据id单条查询*/
			uploadFile = allSearchService.queryFileInformationById(uploadFile);
			response.setContentType("text/html;charset=UTF-8");   
			//String rootpath = request.getSession().getServletContext().getRealPath("/");  
			String rootpath=SysParamInits.ARRANGE_FILE_PATH;//配置文件中指定下载文件路径。例D:\files
			File f = new File(rootpath + File.separator + uploadFile.getFilepath());  
			response.reset();
			response.setContentType("application/x-excel");  
			response.setCharacterEncoding("UTF-8");  
			response.setHeader("Content-Disposition", "attachment; filename=" 
			+ new String(uploadFile.getFilename().getBytes("gbk"),"iso-8859-1"));  
			response.setHeader("Content-Length",String.valueOf(f.length()));
			fis=new FileInputStream(f);
			in = new BufferedInputStream(fis);  
			out = new BufferedOutputStream(response.getOutputStream());  
			byte[] data = new byte[1024];  
			int len = 0;  
			while (-1 != (len=in.read(data, 0, data.length))) {  
				out.write(data, 0, len);  
			}  
		} catch (Exception e) {  
			e.printStackTrace();  
		} finally {  
			if (in != null) {  
				try {
					in.close();
				} catch (IOException e) {
				}  
			}  
			if (out != null) {  
				try {
					out.close();
				} catch (IOException e) {
				}  
			}  
			if (fis != null) {  
				try {
					fis.close();
				} catch (IOException e) {
				}  
			}  
		}  
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小志的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值