SpringBoot项目,打包部署到服务器时,报错:找不到文件路径

原本代码如下

第一步:获取文件路径 

public static String getXmlPath(String type) throws FileNotFoundException, UnknownHostException {
        // 从项目中获取打包config的demo
        File path = new File(ResourceUtils.getURL("classpath:").getPath());
        String absolutePath = path.getAbsolutePath();
        System.out.println(absolutePath);
        if (!path.exists())
            path = new File("");
        System.out.println("path:" + path.getAbsolutePath());
        String pathforpackage = "src" + File.separator + "main" + File.separator + "resources" + File.separator
                + "configdemo" + File.separator + "configforpackagedevelop.xml";
        String oldPath = "target" + File.separator + "classes";
        if (type == "packagedev") {
            String packagepath = absolutePath.replace(oldPath, pathforpackage);
            return packagepath;
        }
        return "";
    }
第二部:根据文件路径读取文件数据
// Java读取本地文件
	public static String getLocalXmlfileFile(String path) {
		
		File file = new File(path);
		StringBuilder localStrBulider = new StringBuilder();
		if (file.isFile() && file.exists()) {
			try {
				InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file), "utf-8");// gbk
				BufferedReader bufferReader = new BufferedReader(inputStreamReader);
				String lineStr = null;
				try {
					while ((lineStr = bufferReader.readLine()) != null) {
						localStrBulider.append(lineStr + "\r\n");
					}
					bufferReader.close();
					inputStreamReader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					System.out.println("file read error!");
					e.printStackTrace();
				}
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				System.out.println("file catch unsupported encoding!");
				e.printStackTrace();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				System.out.println("file not found!");
				e.printStackTrace();
			}
		} else {
			System.out.println("file is not a file or file is not existing!");

		}
		System.out.println("localStrBulider.toString():" + localStrBulider.toString());
		return localStrBulider.toString();
	}

 

 

修改后如下

 

 

public static String getXml(String type) {
        //springboot 中打jar后读取文件,必须用以下方法读取,不然识别不到
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("classpath:configdemo");
        stringBuilder.append(File.separator);
        if (type == "packagedev") {
            stringBuilder.append("configforpackagedevelop.xml");
        }
        String path = stringBuilder.toString();
        Resource fileRource = new ClassPathResource(path);
        InputStream inputStream = null;
        try {
            inputStream = fileRource.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String result = com.shishike.susie.utility.FileUtil.InputStream2String(inputStream);
        return result;
    }

文件路径如下:

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值