Java访问资源

从classpath中读取文件

个人觉得使用Class.getResourceAsStream(path)较为靠谱,Class.getResource(path)在JBoss中使用,获取到的路径会有问题

1
2
3
InputStream in = ResourceDemo.class.getResourceAsStream("aa.properties");
Properties prop = new Properties();
prop.load(in);

对文件(目录)操作(JDK7以后)

使用java.nio.file.Filesjava.nio.file.Pathsjava.nio.file.Path结合比较好

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Path path = Paths.get("/");
Files.walkFileTree(path, new FileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
System.out.println("dir:" + dir);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
System.out.println("file:" + file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
});

本地(内嵌式)DNS服务

参考资料:
Java DNS查询内部实现
Local Managed DNS (Java)
InetAddress类之创建新的InetAddress对象

问题:应用中通过域名或hostname访问外部应用,但dns服务器上又没有它们的ip映射关系
目的:开发一个内嵌式的DNS服务,可通过配置注册hostname和ip的映射关系
步骤:

  1. 在源代码的META-INF/services目录增加一个sun.net.spi.nameservice.NameServiceDescriptor文件,内容为

    1
    io.jasonlu.learning.dnslookup.LocalManagedNameServiceDescriptor
  2. LocalManagedNameServiceDescriptor的内容如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    public class LocalManagedNameServiceDescriptor implements NameServiceDescriptor {
    @Override
    public NameService createNameService() throws Exception {
    return new LocalManagedNameService();
    }
    @Override
    public String getProviderName() {
    return "dnsljh";
    }
    @Override
    public String getType() {
    return "dns";
    }
    public LocalManagedNameServiceDescriptor() {
    System.out.println("hello, you're in LocalManagedNameServiceDescriptor");
    }
    }

LocalManagedNameService主要代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* Created by louz on 2016/12/15.
*/
public class LocalManagedNameService implements NameService {
private Map<String, InetAddress> managedAddresses = new HashMap<>();
public LocalManagedNameService() {
try {
// 此处可通过其他方式初始化managedAddresses
InetAddress address = InetAddress.getByName("10.1.1.1");
managedAddresses.put("mockhost", address);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
@Override
public InetAddress[] lookupAllHostAddr(String s) throws UnknownHostException {
InetAddress inetAddress = managedAddresses.get(s);
if (inetAddress == null) {
throw new UnknownHostException();
}
return new InetAddress[]{inetAddress};
}
@Override
public String getHostByAddr(byte[] bytes) throws UnknownHostException {
throw new UnknownHostException("method not implement yet");
}
}

  1. 在程序的启动参数上增加:

    1
    -Dsun.net.spi.nameservice.provider.1=dns,dnsljh -Dsun.net.spi.nameservice.provider.2=default
  2. 客户端示例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public class DnsLookUpTest {
    @Test
    public void testLookup() throws UnknownHostException {
    String googleHost = InetAddress.getByName("www.google.com").getHostAddress();
    System.out.println(googleHost); // www.google.com没有在managedAddress中,因此按默认方式通过外部DNS获取其对应ip
    String mockhost = InetAddress.getByName("mockhost").getHostAddress();
    // mockhost在managedAddress中,因此获取到其ip为10.1.1.1
    assertThat(mockhost, is("10.1.1.1"));
    }
    }

JDK解析XML

转 Java 语言的 XPath API