欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Vue调用Java接口下载文件流

程序员文章站 2022-05-04 19:46:12
vue下载文件实例定义资源下载接口@PostMapping(resource)public void downloadRes(HttpServletResponse response, @RequestBody String json) {JSONObject param = JSONObject.parseObject(json);downloadHelper.validateDownload(response, param.getInteger(resourceId));}具体实现...

vue下载文件实例

定义资源下载接口

@PostMapping(resource)
public void downloadRes(HttpServletResponse response, @RequestBody String json) {
	JSONObject param = JSONObject.parseObject(json);
	downloadHelper.validateDownload(response, param.getInteger(resourceId));
}

具体实现

public void validateDownload(HttpServletResponse response, Integer resourceId) {
	try (ServletOutputStream outputStream = response.getOutputStream()) {
		response.setContentType(applicationjson;charset=UTF-8);
		// 校验1 - 参数
		if (resourceId == null) {
			log.error(参数有误,无法下载!);
			BaseResult result = BaseResult.error().message([resourceId]参数有误);
			outputStream.write(JSON.toJSONString(result).getBytes(Charset.defaultCharset()));
			return;
		}
		//校验2 - 资源记录
		DataResource resource = dataResourceService.getById(resourceId);
		if (resource == null) {
			log.error(参数有误,无对应资源信息!);
			BaseResult result = BaseResult.error().message(资源记录不存在);
			outputStream.write(JSON.toJSONString(result).getBytes(Charset.defaultCharset()));
			return;
		}
		// 校验3 - 文件
		File file = new File(resource.getPath());
		if (!file.exists()) {
			log.error(资源不存在,无法下载!);
			BaseResult result = BaseResult.error().message(资源不存在);
			outputStream.write(JSON.toJSONString(result).getBytes(Charset.defaultCharset()));
			return;
		}
		// 文件输出
		response.setContentType(applicationoctet-stream;charset=UTF-8);
		response.setHeader(Content-Disposition,
				attachment;filename= + new String(resource.getName().getBytes(UTF-8), iso8859-1));
		response.setContentLength((int) file.length());
		try (FileInputStream fis = new FileInputStream(file)) {
			StreamUtils.copy(fis, outputStream);
		} catch (IOException ex) {
			log.error(输出文件流失败 , ex);
		}
	} catch (IOException ex) {
		log.error(操作异常!, ex);
	}
}

vue 调用

export const axios = {
	downLoadRes  (url = '', params = {}, configure = {}) = {
		  configure['Authorization']= 'Bearer '+ localStorage.token
		  return new Promise((resolve,reject) = {
			Axios.post(url,params,{
				  headersconfigure,
				  responseType'blob'
			}).then(response = {
				const { data } = response;
				const reader = new FileReader();
				const filename = params['filename'];
				if(data.type === 'applicationjson'){
					reader.readAsText(data);
					reader.onload = function () {
						const { message, code } = JSON.parse(reader.result);
						console.log(message + code);
					}
				}else{
					var link = document.createElement('a');
					var blob = new Blob([response.data]);
					if('download' in link) {
						link.download = filename;
						link.style.display = 'none';
						link.href = URL.createObjectURL(blob);
						document.body.appendChild(link);
						link.click(); //执行下载
						URL.revokeObjectURL(link.href); //释放url
						document.body.removeChild(link); //释放标签
					} else {
						navigator.msSaveBlob(blob, filename)
					}
				}
			}).catch(err = {
				console.error(err);	
			})
		})
	}
}

本文地址:https://blog.csdn.net/lwjxxhx/article/details/107504550