生活随笔
收集整理的这篇文章主要介绍了
spring boot结合FastDFSClient做下载文件注意事项
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
spring boot结合FastDFSClient做下载文件注意事项
1.后台下载方法走完后,前端页面浏览器一直没出现下载框。 2.ie浏览器兼容问题。
下面的FastDFSClient类依赖fdfsclient-jar-with-dependencies.jar包 下面是后台代码。
@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 ( ) ; } }
下面是上面代码的解释。 下面是FileUtil.downloadFileByEncode_gb2312方法。
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" ) ; response
. setHeader ( "Content-Disposition" , "attachment; filename=" + fileName
) ; bos
. write ( data
, 0 , data
. length
) ; } catch ( Exception ex
) { ex
. printStackTrace ( ) ; throw new RuntimeException ( ex
. getMessage ( ) ) ; } finally { 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.后台下载方法走完后,前端页面浏览器一直没出现下载框。 这是之前的前端下载代码。
$
. 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提交可以出现下载提示框,于是改成下面的方式,就可以在下载文件后看到下载提示框了。
$form
= $ ( '<form method="post"></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)在工具和扩展中找到了,迅雷下载支持,并且是启用的,然后禁用它。 (3)重启 IE之后:进行下载操作,结果一切正常,问题解决: 下面是ie浏览器设置的参考网址。 https://www.cnblogs.com/beijixingzhiguang/p/4990984.html
这样可以正常下载了,但没有返回到success和error方法中,这个还有待解决。
总结
以上是生活随笔 为你收集整理的spring boot结合FastDFSClient做下载文件注意事项 的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得生活随笔 网站内容还不错,欢迎将生活随笔 推荐给好友。