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

 















免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM