Spring MVC 通过穿过来的文件名(带后缀)下载文件-过程中遇到的坑

需要通过地址下载一个文件:

http://localhost:9025/download/b.csv

后台代码:

    @RequestMapping(
            value = {"/download/{filename:}"},
            method = {RequestMethod.GET}, produces = {"application/json"}
            )
    public ResponseEntity<BigDecimal> download(
            @PathVariable(value = "filename") String filename, HttpServletRequest request, HttpServletResponse response) throws NotFoundException{
        try {
            
            logger.info(id);
            File f = new File("E:/" + filename);
            downLoad(f, response);
        } catch (Exception e) {
            logger.info(e.getMessage(), e);
        }
        return null;
    }

访问地址,报错:406 Not Acceptable;一顿查询,看到文章https://blog.csdn.net/allenChenZhiMing/article/details/82015932

需要增加@CrossOrigin(origins = "*", maxAge = 3600),调整produces = {},调整代码后:

    @RequestMapping(
            value = {"/download/{filename:.+}"},
            method = {RequestMethod.GET}, produces = {}
            )
    @CrossOrigin(origins = "*", maxAge = 3600)
    public ResponseEntity<BigDecimal> download(
            @PathVariable(value = "filename") String filename, HttpServletRequest request, HttpServletResponse response) throws NotFoundException{
        try {
            
            logger.info(id);
            File f = new File("E:/" + filename);
            downLoad(f, response);
        } catch (Exception e) {
            logger.info(e.getMessage(), e);
        }
        
        return null;
        
    }

 

再次访问地址:发现后台接收到的文件名不带后缀,点(.)后边的被截取。再次查询,看到文章:https://blog.csdn.net/liming19890713/article/details/78417995

调整@RequestMapping(
            value = {"/test/{id:.+}"},
            method = {RequestMethod.GET}, produces = {}
            )

再次访问,成功下载文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值