博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
文件下载
阅读量:4182 次
发布时间:2019-05-26

本文共 2876 字,大约阅读时间需要 9 分钟。

方法一

@RequestMapping("download")    public ResponseEntity
download(String filePath, int id) throws IOException {
client = dcf.createClient(WSDL_URL); FileDecryptVo fileDecryptVo=null; try {
objects = client.invoke("getFileDecryptById", id); String json = (String) objects[0]; fileDecryptVo = JSON.parseObject(json, FileDecryptVo.class); } catch (Exception e) {
e.printStackTrace(); } assert fileDecryptVo != null; String filepath =fileDecryptVo.getFilePath(); File file=new File(filepath); String fileName=new String(filepath.split("\\\\")[2].getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1); HttpHeaders headers = new HttpHeaders(); fileName=new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1); headers.setContentDispositionFormData("attachment", fileName); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); return new ResponseEntity
(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED); }

方法二

@RequestMapping(value = "/download", method = RequestMethod.GET)    public void download(HttpServletResponse res, int id) {
client = dcf.createClient(WSDL_URL); FileDecryptVo fileDecryptVo = null; try {
objects = client.invoke("getFileDecryptById", id); String json = (String) objects[0]; fileDecryptVo = JSON.parseObject(json, FileDecryptVo.class); } catch (Exception e) {
e.printStackTrace(); } assert fileDecryptVo != null; String filepath = fileDecryptVo.getFilePath(); res.setContentType(MediaType.TEXT_PLAIN_VALUE); //UTF_8是项目编码,ISO_8859_1是浏览器默认编码。 String fileName = new String(filepath.split("\\\\")[2].getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1); res.setHeader("Content-Disposition", "attachment;filename=" + fileName); byte[] buff = new byte[2048]; BufferedInputStream bis = null; BufferedOutputStream bos = null; try {
bis =new BufferedInputStream(new FileInputStream(new File(filepath))) ; bos = new BufferedOutputStream(res.getOutputStream()); int readLength=0; while ((readLength=bis.read(buff))!= -1) {
bos.write(buff, 0, readLength); bos.flush(); } } catch (IOException e) {
e.printStackTrace(); } finally {
try {
assert bis != null; bis.close(); assert bos != null; bos.close(); } catch (IOException e) {
e.printStackTrace(); } } System.out.println("success"); }

转载地址:http://owgai.baihongyu.com/

你可能感兴趣的文章
微信QQ等主流应用上线国产系统UOS:界面曝光
查看>>
3天,让你的C++从入门到精通
查看>>
这个「微信年度报告」你被骗了吗?
查看>>
可怕啊,阿里面试!
查看>>
疯传!某大厂P8面试题库遭到泄露!
查看>>
中国编程第一人,一人抵一城!
查看>>
【微信红包封面】最新!最全!
查看>>
最近线上发生的两个坑爹锅!
查看>>
腾讯QQ的回应来了~
查看>>
最新5款微信红包封面,来了 !!!
查看>>
哦嚯,被微服务给坑了...
查看>>
SCI审稿人亲授:机器学习在智能化时代下的应用
查看>>
安卓微信 8.0 内测版来啦!
查看>>
我劝你不要再留QQ邮箱了
查看>>
真香!用 4K 高清显示器写代码!(包邮送一台)
查看>>
神器!各行业必备!低调使用
查看>>
B 站,牛逼!
查看>>
微信状态视频、图片素材来啦!
查看>>
再见了!锤子!!!
查看>>
LeetCode 全站第一,牛逼!
查看>>