关于项目部署到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();
        }
    }

你可能感兴趣的:(download,java)