springmvc下载

一、发起请求的JS

    上传JS才用拼接一个form表单的形势来发起请求:

function downloadFileJQ(url, params, method, datas) {
	if (url && params) {
		var inputs = '';
		for (var key in params) {
			var param = params[key];
			inputs += '<input type="hidden" name="' + key + '"/>';
		}
		if (datas) {
			inputs += '<input type="hidden" name="table_datas"/>';
		}
		var $form = $('<form style="display:none" action="' + url + '" method="' + 
				(method || 'POST') + '">' + inputs + '</form>');
		$form.appendTo('body');
		for (var key in params) {
			var param = params[key];
			$form.find('input[name="' + key + '"]').val(param);
		}
		if (datas) {
			$form.find('input[name="table_datas"]').val(encodeParam(datas));
		}
		$form.submit();
		$form.remove();
	}
}

function encodeParam(paramObj, views) {
	return JSON.stringify(paramObj);
}

URL:请求地址;params:需要传入的参数;method:是POST请求还是GET请求;datas:别的数据

二、java写法

①才用spring的方式处理请求

@RequestMapping(value="/downloadTemplate.action",method=RequestMethod.GET)
public ResponseEntity<byte[]> download(HttpServletRequest request,HttpServletResponse response,@RequestParam("type")String type){
	try {
		String filePath = request.getSession().getServletContext().getRealPath("") + File.separator + "机顶盒资源模板.xlsx";
		String title = "机顶盒资源模板.xlsx";
		File file = new File(filePath);//新建一个文件
		HttpHeaders headers = new HttpHeaders();//http头信息
		String downloadFileName = new String(title.getBytes("UTF-8"),"iso-8859-1");//设置编码
		headers.setContentDispositionFormData("attachment", downloadFileName);
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		//MediaType:互联网媒介类型  contentType:具体请求中的媒体类型信息
		return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers,HttpStatus.CREATED);
	} catch (Exception e) {
		e.printStackTrace();
	}
	return null;
}

②一般方式处理请求

@RequestMapping(value="/downloadTemplate2.action",method=RequestMethod.GET)
public void downloadTemplate(HttpServletRequest request,HttpServletResponse response) throws Exception {
String filePath = request.getSession().getServletContext().getRealPath("") + File.separator + "xxx.xlsx";
File file = new File(filePath);//新建一个文件
if (file.exists()) {
	response.setContentType("application/force-download");// 设置强制下载不打开
	response.addHeader("Content-Disposition","attachment;fileName=" + new String("xxx.xlsx".getBytes("ISO-8859-1"), "UTF-8"));
	byte[] buffer = new byte[1024];
	FileInputStream fis = null;
	BufferedInputStream bis = null;
	try {
		fis = new FileInputStream(file);
		bis = new BufferedInputStream(fis);
		OutputStream os = response.getOutputStream();
		int i = bis.read(buffer);
		while (i != -1) {
			os.write(buffer, 0, i);
			i = bis.read(buffer);
		}
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		if (bis != null) {
			try {
				bis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (fis != null) {
			try {
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
  }
}

③使用common.io的IOUtils下载

@RequestMapping("/downloadTemplate3.action")
@ResponseBody
public void downloadTemp(HttpServletResponse response,HttpServletRequest request) throws IOException {
	response.setHeader("Content-Disposition", "attchment;filename="+new String("机顶盒资源模板.xlsx".getBytes("UTF-8"),"iso-8859-1"));
	ServletOutputStream outputStream = response.getOutputStream();
	String file = request.getSession().getServletContext().getRealPath("") + "template" + File.separator + "机顶盒资源模板.xlsx";
	FileInputStream inputStream = new FileInputStream(file);
	IOUtils.copy(inputStream, outputStream);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值