java:使用.class.getClassLoader()得到文件路径乱码

出现的问题

使用:

类.class.getClassLoader().getResource("*")).getPath();

得到文件路径出现乱码:
原代码:

package com.cjl.jsoup;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Objects;

public class JsoupDemo1 {
    
    
    public static void main(String[] args) {
    
    
        String path = Objects.requireNonNull(JsoupDemo1.class.getClassLoader().getResource("student.xml")).getPath();
        try {
    
    
            Document document = Jsoup.parse(new File(path), "utf-8");
            Elements elements = document.getElementsByTag("name");
            System.out.println(elements.size());
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
    }
}

出现文件找不到的异常:

java.io.FileNotFoundException: C:\Users\24550\eclipse-workspace\IDEA%e6%b5%8b%e8%af%95%e7%bb%83%e4%b9%a0\out\production\IDEA%e6%b5%8b%e8%af%95%e7%bb%83%e4%b9%a0\student.xml (系统找不到指定的路径。)
	at java.io.FileInputStream.open0(Native Method)
	at java.io.FileInputStream.open(FileInputStream.java:195)
	at java.io.FileInputStream.<init>(FileInputStream.java:138)
	at org.jsoup.helper.DataUtil.load(DataUtil.java:51)
	at org.jsoup.Jsoup.parse(Jsoup.java:103)
	at com.cjl.jsoup.JsoupDemo1.main(JsoupDemo1.java:17)

解决方法:

对path进行修改:

path = java.net.URLDecoder.decode(path, "utf-8");

原因:

URL的编码和解码的问题,地址用Encode编码要用与URLDecode进行解码。

成功:

修复后代码:

package com.cjl.jsoup;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.File;
import java.util.Objects;

public class JsoupDemo1 {
    
    
    public static void main(String[] args) {
    
    
        String path = Objects.requireNonNull(JsoupDemo1.class.getClassLoader().getResource("student.xml")).getPath();
        try {
    
    
            path = java.net.URLDecoder.decode(path, "utf-8");
            Document document = Jsoup.parse(new File(path), "utf-8");
            Elements elements = document.getElementsByTag("name");
            System.out.println(elements.size());
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_19265749/article/details/107050279
今日推荐