关于项目部署到linux,实现下载功能访问不到resource下的文件的问题

写了一段代码,在自己本地可以跑,部署到linux上就找不到文件了,原来是相对路径在linux上找不到,在linux下读取resource下的文件就只能使用流来读取文件,只能用流读取

InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("helpCenterFiles/" + fileName);
    try {
       // InputStream inputStream = resource.getInputStream();
        String userAgent = request.getHeader("User-Agent");
        // 针对IE或者以IE为内核的浏览器:  n
        if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
            fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
        } else {
        // 非IE浏览器的处理:
            fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
        }
        response.setContentType("multipart/form-data");
        response.addHeader("Content-Disposition","attachment;fileName=" + fileName);
        out = response.getOutputStream();
        //读取文件流
        int len = 0;
        byte[] buffer = new byte[1024 * 10];
        while ((len = inputStream.read(buffer)) != -1){
            out.write(buffer,0,len);
        }
        out.flush();
    }catch (Exception e){
        e.printStackTrace();
    }finally {
        try {
            out.close();
        } catch (IOException e) {
            System.out.println("关闭流出现异常");
            e.printStackTrace();
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值