其他分享
首页 > 其他分享> > application/force-download 不生效

application/force-download 不生效

作者:互联网

不管用什么方式都无法下载txt 设置application/force-download也不生效  很无奈
胡搞瞎搞  最终解决方案:但是没搞明白什么原理 问题解决
@RequestMapping(value = "/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);

}

 

标签:outputStream,force,String,fileName,application,download,new,response,fileId
来源: https://www.cnblogs.com/Ai-Hen-Jiao-zhi/p/10557574.html