金鞍玉勒寻芳客,未信我庐别有春。这篇文章主要讲述application/force-download 不生效相关的知识,希望能为你提供帮助。
不管用什么方式都无法下载txt 设置application/force-download也不生效很无奈
胡搞瞎搞最终解决方案:但是没搞明白什么原理 问题解决
@RequestMapping(value = "https://www.songbingjia.com/weChatDown/{fileId}") public ResponseEntity< byte[]> weChatFiledownload(@PathVariable("fileId") String fileId, HttpServletResponse response, HttpServletRequest request) throws Exception { DsCmsFile dsCmsFile = fileService.get(fileId); if (dsCmsFile != null) { String filePath = dsCmsFile.getFileUrl(); String fileName = dsCmsFile.getFileName(); File file = new File(filePath); long size = file.length(); //为了解决中文名称乱码问题 这里是设置文件下载后的名称 fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1"); response.reset(); response.setHeader("Accept-Ranges", "bytes"); //设置文件下载是以附件的形式下载 response.setHeader("Content-disposition", String.format("attachment; filename="%s"", fileName)); response.addHeader("Content-Length", String.valueOf(size)); ServletOutputStream sos = response.getOutputStream(); FileInputStream in = new FileInputStream(file); BufferedOutputStream outputStream = new BufferedOutputStream(sos); byte[] b = new byte[1024]; int i = 0; while ((i = in.read(b)) > 0) { outputStream.write(b, 0, i); } outputStream.flush(); sos.close(); outputStream.close(); in.close(); } return new ResponseEntity< > (HttpStatus.OK); }
【application/force-download 不生效】
推荐阅读
- this application was created using the trial version of the xtrareports 解决方法
- Android四大组件六大布局和五大数据存储
- JDK 11,Tomcat卡在Deploying web application directory
- 使用AppDomain进行动态加载和卸载dll
- git推送报错( No path specified. See 'man git-pull' for valid url syntax或does not appear to be a)
- A.Little Sub and Applese
- Centos使用natapp教程
- androidstudio导入新项目build tools不符合问题解决
- Java内存模型与Volatile,Happen-Before原则等