java调用IPFS去中心化体系

Maven pom.xml引入

<repositories>
 <repository>
 <id>jitpack.io</id>
 <url>https://jitpack.io</url>
 </repository>
 </repositories>
 <dependencies>
 <dependency>
 <groupId>com.github.ipfs</groupId>
 <artifactId>java-ipfs-api</artifactId>
 <version>$LATEST_VERSION</version>
 </dependency>
 </dependencies>

创建节点

IPFS ipfs = new IPFS(new MultiAddress("/ip4/127.0.0.1/tcp/8888"));

初始化IPFS 加载

ipfs.refs.local();

要添加文件使用
NamedStreamable.FileWrapper file = new NamedStreamable.FileWrapper(new File("D:/longpizi.png"));
添加文件到IPFS返回HASH值
Multihash addResult = ipfs.add(file).hash;
输出HASH值
System.out.println(addResult);

查询IPFS里面的文件(通过HASH值查询)

Multihash filePointer =Multihash.fromBase58("HASH值");
byte[] data = ipfs.cat(filePointer);通过HASH值查询文件转为byte[]

通过文件流输出
InputStream inputStream=new ByteArrayInputStream(data);
OutputStream os = response.getOutputStream();
byte[] buffer = new byte[400];
int length = 0;
while ((length = inputStream.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
os.flush();
os.close();
完成的java调用IPFS


未经同意,请勿转载!
 

 














猜你喜欢

转载自www.cnblogs.com/longpizi/p/9160923.html