使用Fastdfs上传文件时出现中文乱码。工具类中的this.getClass().getResource(“/“).getPath()获取配置文件的路径时中文乱码。

博客讲述了在Java项目中使用FastDFS客户端进行文件上传时遇到的异常问题。由于配置文件路径包含中文导致乱码,从而引发了org.csource.common.MyException。解决方案是通过URLDecoder.decode()方法对路径进行解码,确保配置文件路径的正确读取。源代码和工具类中的关键部分被展示,并提供了修改后的代码片段。
摘要由CSDN通过智能技术生成

报错:

Exception in thread "main" org.csource.common.MyException: item "tracker_server" in /D:/%e6%a1%8c%e9%9d%a2/javaDemo/ZG_Four/dongyimai-parent/dongyimai-service/dongyimai-file-service/target/classes/fastdfs_client.conf not found
	at org.csource.fastdfs.ClientGlobal.init(ClientGlobal.java:96)
	at com.util.FastDFSClient.<init>(FastDFSClient.java:24)
	at com.file.FastDfsTest.main(FastDfsTest.java:45)

源代码:

/        使用工具类实现文件上传
        String imgPath2="D:\\桌面\\图书\\A-746.jpg";
        FastDFSClient fastDFSClient = new FastDFSClient("classpath:fastdfs_client.conf");
         path = fastDFSClient.uploadFile(imgPath2, "jpg",null);
        System.out.println(path);

工具类中出现乱码的部分代码:

if (conf.contains("classpath:")) {
			conf = conf.replace("classpath:", this.getClass().getResource("/").getPath());
		}
		ClientGlobal.init(conf);

因为项目是在D:\桌面\javaDemo的路径下。所以在获取配置文件的绝对地址的时候路径中出现中文导致乱码。

解决方法:

获取配置文件成功之后加入该代码conf = URLDecoder.decode(conf,"utf-8");

即:

if (conf.contains("classpath:")) {
			conf = conf.replace("classpath:", this.getClass().getResource("/").getPath());
			conf = URLDecoder.decode(conf,"utf-8");
		}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值