提供图片下载接口

1.浏览器请求地址:

localhost:8080/download/img?path=home/ccsp/img/abc.jpg

2.后台控制层

/**
 * 提供下载图片的接口
 * @author: ljg
 * @Datetime: 2019年8月7日下午6:13:53
 * @ClassName:DownloadfilesController
 */
@Controller
@RequestMapping("download")
public class DownloadfilesController {
    
    @RequestMapping("/img")
    @ResponseBody
    public void init(HttpServletResponse response, @RequestParam("path")String path) {
        try {
            // 文件名
            String fileName=path.substring(path.lastIndexOf("/")+1);
            // 后缀名
            String fileSuffixName=   fileName.substring(fileName.lastIndexOf(".")+1);
            // 清除缓存
            response.reset();
            FileInputStream fileInputStream = new FileInputStream(path);
            ServletOutputStream outputStream = response.getOutputStream();
            response.setContentType("application/" +fileSuffixName + ";" +"charset = UTF-8"); //设置字符集和文件后
            response.setHeader("Content-Disposition", "attachment;filename=" +fileName+"."+fileSuffixName);
            byte[] bytes = new byte[1024];
            while ((fileInputStream.read(bytes)) != -1) {
                outputStream.write(bytes);
            }
            fileInputStream.close();
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

3.取消登录URL拦截。

@WebFilter(filterName = "UrlFilter", urlPatterns = "/*", initParams = {@WebInitParam(name = "noFilterPath", value = "/download/img;...) })

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值