spring boot结合FastDFSClient做下载文件注意事项

spring boot结合FastDFSClient做下载文件注意事项

1.后台下载方法走完后,前端页面浏览器一直没出现下载框。
2.ie浏览器兼容问题。

下面的FastDFSClient类依赖fdfsclient-jar-with-dependencies.jar包
下面是后台代码。

// An highlighted block
/**
	 * 下载
	 * @return
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping(value="/downloadXmlFileList", method={RequestMethod.GET,RequestMethod.POST},produces="text/html;charset=utf-8")
	@ApiOperation(value="下载", notes="下载",response=Long.class)
	public void downloadXmlFileList(HttpServletResponse response,String ids){
		ResultBean<String> rs = null;
		try {
			List<ImportXmlRecordData> importXmlRecordDataList = importXmlRecordService.getAllFdfsclientfileidByIds(ids);
			if(importXmlRecordDataList != null && !importXmlRecordDataList.isEmpty() && importXmlRecordDataList.size() > 0) {
				ImportXmlRecordData importXmlRecordData = importXmlRecordDataList.get(0);
				byte[] data =  FastDFSClient.downloadFile(importXmlRecordData.getFdfsclientfileid());
		    	FileUtil.downloadFileByEncode_gb2312(response, data, importXmlRecordData.getFilename());
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
	}

下面是上面代码的解释。
spring boot结合FastDFSClient做下载文件注意事项_第1张图片
下面是FileUtil.downloadFileByEncode_gb2312方法。

// An highlighted block
/**
	 * 文件下载
	 * @param response
	 * @param downloadFile
	 */
	public static void downloadFileByEncode_gb2312(HttpServletResponse response, byte[] data, String showFileName) {
		
		BufferedInputStream bis = null;
		OutputStream os = null;
		BufferedOutputStream bos = null;
		try {
	    os = response.getOutputStream(); // 重点突出
	    bos = new BufferedOutputStream(os);
	    // 对文件名进行编码处理中文问题
	    String fileName = new String( showFileName.getBytes("gb2312"), "ISO8859-1"); 
	    response.reset(); // 重点突出
	    response.setCharacterEncoding("UTF-8"); // 重点突出
	    response.setContentType("application/x-msdownload");// 不同类型的文件对应不同的MIME类型 // 重点突出
	    // inline在浏览器中直接显示,不提示用户下载
	    // attachment弹出对话框,提示用户进行下载保存本地
	    // 默认为inline方式
	    response.setHeader("Content-Disposition", "attachment; filename="+fileName); // 重点突出
	    bos.write(data, 0, data.length);// 将文件发送到客户端
	    
	} catch (Exception ex) {
		ex.printStackTrace();
		throw new RuntimeException(ex.getMessage());
	} finally {
		// 特别重要
	    // 1. 进行关闭是为了释放资源
	    // 2. 进行关闭会自动执行flush方法清空缓冲区内容
			try {
				if (null != bis) {
					bis.close();
					bis = null;
				}
				if (null != bos) {
					bos.close();
					bos = null;
				}
				if (null != os) {
					os.close();
					os = null;
				}
			} catch (Exception ex) {
				throw new RuntimeException(ex.getMessage());
			}
		}
	}

以上都是后台的代码

下面是前台代码

前台代码出现了两个问题。
1.后台下载方法走完后,前端页面浏览器一直没出现下载框。
这是之前的前端下载代码。

// An highlighted block
$.form({
		type: "GET",
		dataType: 'text',
		async: true,
		url: '<%=basePath%>importXmlRecord/downloadXmlFileList?ids='+checktdArr,
		success:function(respose){
			debugger;
			$.messager.alert('提示',"下载成功!",'success');
		},
		error:function(respose){
			debugger;
			$.messager.alert('提示',respose.msg,'error');
		}
	 })

在网上查了一下,这样提交相当于ajax提交,ajax提交后看不到下载框,要用form提交可以出现下载提示框,于是改成下面的方式,就可以在下载文件后看到下载提示框了。

// An highlighted block
$form = $('
'
).appendTo('body'); var url = '<%=basePath%>importXmlRecord/downloadXmlFileList'; $form.form('submit', { url: url, dataType: 'text', onSubmit:function(para){ para.ids = checktdArr; }, success:function(respose){ $.messager.alert('提示',"下载成功!",'success'); }, error:function(respose){ $.messager.alert('提示',"下载失败!",'error'); } }); $form.remove();

2.谷哥浏览器可以正常下载后,去ie浏览器上试了一下,ie不能正常下载,而且点击下载按钮后总是会出现迅雷下载框提示下载与文件无关的东西,并且后台报ClientAbortException:java.io.IOException错误。去网上查了一下,设置了一下ie浏览器的管理下载项,ie浏览器就可以正常下载了。
(1)Internet选项,打开 ”管理下载项“。
(2)在工具和扩展中找到了,迅雷下载支持,并且是启用的,然后禁用它。
spring boot结合FastDFSClient做下载文件注意事项_第2张图片
(3)重启 IE之后:进行下载操作,结果一切正常,问题解决:
在这里插入图片描述
下面是ie浏览器设置的参考网址。
https://www.cnblogs.com/beijixingzhiguang/p/4990984.html

这样可以正常下载了,但没有返回到success和error方法中,这个还有待解决。

你可能感兴趣的:(spring,boot)