java后端实现下载接口,出现Invalid character in header content [“Content-Disposition“]解决方法

java后端实现下载接口

public void download(HttpServletResponse response, String id) {
        List<ProjectVo> projectList = projectManagemenet.getProjectList();
        String filepath = "";
        ServletOutputStream ops = null;
        for (ProjectVo projectVo : projectList) {
            if (projectVo.getUid().equals(id)){
                File file = new File(path);
                filepath = file.getParentFile() + "/" + projectVo.getPath();
                response.setContentType("application/octet-stream");
                response.setCharacterEncoding("utf-8");
                response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(projectVo.getName()) +".sgp");

                response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
                File file1 = new File(filepath);
                try {
                    InputStream inputStream = new FileInputStream(file1);
                    ops = response.getOutputStream();
                    IOUtils.copy(inputStream,ops);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }

直接使用项目名作为文件名,因为项目名需要转码,所以使用java自带的URLEncoder.encode()进行转码

后端使用URLEncode进行编码传输的时候,传到前端还需要前端进行解码一下即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值