springboot 不识别resource

# 情景
今天在做上传文件的时候,遇到在window开发环境下启动的服务,服务正常
但是当打好包之后,就不行了,报错找不到路径,查看之后,和打包方式和路径写法都有关系


下面就将我这边的window和linux通用的方案,给出来,希望能帮到大家


# 注意
我这里是springboot,打的war包,
打jar包的时候如果默认的方式,报找不到路径,可以使用自定义maven打包方式
# 读写通用方案,如下所示,以下内容仅供参考,希望客官能够找到方法中的重点


# 读取
/*
     * @Author: tianyong
     * @Date: 2020/10/14 14:29
     * @Description: 流操作文本
     */
    public static String readTxt(){
        // 创建输入输出流
        BufferedReader reader = null;
        try {
            InputStream is = new ClassPathResource(Constants.TXT_PATH).getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8.name()));
            // 开始写文件
            String data = reader.readLine();
            reader.close();
            return data;
        } catch (Exception e) {
            log.info("文本文件读取操作失败");
            e.printStackTrace();
        }
        return "";
    }


# 写入
/*
     * @Author: tianyong
     * @Date: 2020/10/14 10:32
     * @Description: 系统设置上传数据上传接口
     */
    @CrossOrigin
    @ResponseBody
    @PostMapping("/upload")
    public Map uploadSystemSetting (SystemSetting ss) throws FileNotFoundException {
        Map<String,Object> result = new HashMap(1);
        if(ss != null){
            String path = ResourceUtils.getURL("classpath:").getPath();
            // 变量接收
            MultipartFile file = ss.getFile();
            // 设置默认图片目录
            String txtPath = "/static/images";
            ClassPathResource images = new ClassPathResource("\\" + path + "\\static\\images");
            File imgResource = new File(images.getPath());
            ClassPathResource txt = new ClassPathResource("\\" + path + "\\static\\txt\\data.txt");
            File txtResource = new File(txt.getPath());
            if(imgResource.exists() && txtResource.exists()){
                Utils.writeImg(imgResource,file);
                Utils.writeTxt(txtResource,ss,txtPath);
                result.put("state", true);
                return result;
            }
        }
        result.put("message","系统设置上传数据接口异常,请联系管理员");
        return result;
    }





/*
     * @Author: tianyong
     * @Date: 2020/10/14 14:05
     * @Description: 流操作图片
     */
    public static void writeImg(File resource, MultipartFile file){
        // 获得存储的静态资源路径
        String path = resource.getAbsolutePath();
        // 获取上传文件名
        String filename = file.getOriginalFilename();
        // 创建file对象
        File toFile = new File(path + File.separator + filename);
        // 创建输入输出流
        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(new FileOutputStream(toFile));
            // 开始写文件
            bos.write(file.getBytes());
            bos.flush();
            bos.close();
        } catch (Exception e) {
            log.info("图片文件写入操作失败");
            e.printStackTrace();
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值